03-Lists and tuples

ipynb38 kBPython · 94 cellsSlides of chapter 0

03 - Lists and Tuples

Jupyter notebooks

This is a space where text and code come together to form a coherent document. Throughout the course, we will make use of Jupyter notebooks for illustration purposes and for experimentation.

Motivation

After the second set of slides, you should be able to write straight-line programs and programs that allow for branching and repetition based on an expression's truth value. What we have learned so far is fundamental, useful, but not enough. In particular, programming often requires storing in memory user inputs or copying existing elements and storing them in an object with some internal structure.

For example, given a sequence of numbers entered by user, how to:

  1. Print them in reverse order?

  2. Check if they are distinct?

  3. Sort them in (reverse) order?

This is where we will make use of the list data structure, which is built-in in Python.

List

list initialization

In Python, we use brackets [] to initialize a list, where elements are separated by commas. For example, the below objects are two different lists:

In [1]
L1 = [1, 2, 3, 4, 5]
L2 = ["hello", 2.0, 'CSC243', -1, True]

A Python list is either:

  1. Homogeneous, thus containing objects of the same data-type (like L1)

  2. Heterogeneous, containing objects of mixed data-type (like L2)

Like in str, the built-in len() function gives the length of a list.

In [2]
n = len(L2)
print(n)
5

list indexing

The list elements can be accessed by specifying the index:

  1. Non-negative indices: start at 0, end at len()-1.
In [3]
# illustrating positive indices
first_element = L2[0]
last_element = L2[n-1]

print("first element of L2:", first_element)
print("last element of L2:", last_element)
first element of L2: hello
last element of L2: True
  1. Negative indices work from right to left (reverse order).
In [4]
# illustrating negative indices
# n = len(L2)
first_element_right = L2[-1] # L2[n-1]
third_element_right = L2[-3] # L3[n-3]

print("first element from the right", first_element_right)
print("third element from the right", third_element_right)
first element from the right True
third element from the right CSC243

Lists are mutable data-structures (i.e., can be modified). We not only have read-access, but also write-access. Let's modify elements from L2 at different indices.

In [5]
print("L2 (before):", L2)
L2[1] = L2[1] + 2
L2[0] = "goodbye"
print("L2 (after):", L2)
L2 (before): ['hello', 2.0, 'CSC243', -1, True]
L2 (after): ['goodbye', 4.0, 'CSC243', -1, True]

list slicing

In addition to single-element access, we can slice a list using the L[start:end] syntax, which slices the list at index i=start, start + 1, start+2, .... , end-1. Notice that as in the stop parameter in the range, the end is exclusive as well. The default values are start=0 and end=n where n=len(L).

In [6]
L = [1, 2, 3, 4, 5, 6]
print("slice 1", L[:]) # without any paramters
print("slice 2", L[1:3]) # start = 1, stop =3
print("slice 3", L[:3]) # without the start parameter
print("slice 4", L[3:]) # without the stop paremeter
slice 1 [1, 2, 3, 4, 5, 6]
slice 2 [2, 3]
slice 3 [1, 2, 3]
slice 4 [4, 5, 6]

We can also specify a step for the slicing, using L[start:stop:step]. This will extract values at indices i = start, start+step, start + 2*step, … , start + c*step, where c is an integer such that start + c*step <= n-1.

In [7]
print("odd entries of L", L[::2])
print("even entries of L", L[1::2])
odd entries of L [1, 3, 5]
even entries of L [2, 4, 6]

Low-level implementation

List are stored in memory block consisting of contiguous memory cells (called arrays in other languages such as C/C++). This gives the indexing operator [] direct access: given index i, we can directly go to L[i] (compared to starting from the beginning of the list and moving i steps, which is not efficient). Direct access is very fast.

Homogeneous list initialization (via * operator)

Using the * operator, we can initialize a list with an element repeated multiple times. General syntax: L = [value]*n, where n is an integer.

Examples:

In [8]
L = ['test']*3
print(L)
L = [-1]*10
print(L)
L = [True]*5
print(L)
['test', 'test', 'test']
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
[True, True, True, True, True]

Homogeneous list initialization via user input (method 1)

Also, we can retrieve input from a user to initialize a list of, say, float. When the number of elements is known, the method uses a for loop (or a counter-controlled while loop) with the following steps:

  1. We take the number of integers n and convert to int (as usual).

  2. We initialize a list to n zeros using: L = [0]*n.

  3. We loop n times and insert each new integer at index i, i.e. at L[i], where i is the loop counter.

Code illustration:

In [9]
n = int(input("Enter the number of integers: "))
L = [0]*n

for i in range(n):
    L[i] = float(input("enter a real number: "))

print(L)
Enter the number of integers: 3
enter a real number: 1.5
enter a real number: 2.5
enter a real number: 3.5
[1.5, 2.5, 3.5]

Homogeneous list initialization via user input (method 2)

If the number of elements is not known ahead of time (as with method 1), we can enter the elements separated by a particular separator -- like the whitespace -- as a string. The approach will split the string using the .split method. Finally, we convert each entry in the resulting list from string to int or float.

In [10]
sep = input("Enter a separator: ")
st = input("Enter numbers seperated by a \'" + str(sep) + "\': ")
L = st.split(sep)
print("L before conversion:", L)

n = len(L)
for i in range(n):
    L[i] = float(L[i])
    
print("L after conversion:", L)
Enter a separator: ,
Enter numbers seperated by a ',': 1,2,3.5
L before conversion: ['1', '2', '3.5']
L after conversion: [1.0, 2.0, 3.5]

Example: printing a list in reverse order

Using repetition, we can print in reverse order of elements in a list.

In [11]
print("printing in reverse (method 1):")
for i in range(1, n+1):
    print(L[n-i])

print("printing in reverse (method 2):")
for i in range(n-1, -1, -1):
    print(L[i])
printing in reverse (method 1):
3.5
2.0
1.0
printing in reverse (method 2):
3.5
2.0
1.0

Example: element distinctness problem

Using repetition, we can check if the elements of a list are distinct. First, store user input in length-n list L. The, we need to check if there exists distinct indices i and j between (inclusive) 0 and n-1 such that L[i] == L[j].

We make use of the “True-until-proven-False” pattern:

  1. Assume list consists of distinct elements: use a boolean variable distinct, initialized to True.

  2. Look for evidence of non-distinctness: use nested loops, an outer loop on i and inner loop on j.

  3. If i!=j and L[i]==L[j], set distinct to False and break both loops.

In [12]
# user-input
st = input("Enter integers separated by whitespaces: ")
L = st.split(" ")
n = len(L)
for i in range(n):
    L[i] = int(L[i])

# main work: true-until-false pattern
distinct = True
for i in range(n):
    for j in range(n):
        if i == j:
            continue
        elif L[i] == L[j]:
            distinct = False
            break # from inner loop
    if not distinct:
        break # from outer loop

# display of result
if distinct:
    output_message = "elements in L are distinct"
else:
    output_message = "elements are not distinct"
print(output_message)            
Enter integers separated by whitespaces: 3 1 4 1 5
elements are not distinct

Can do it faster by assuming without loss of generality that i < j. The checks L[i] == L[j] and L[j] == L[i] are redundant.

In [13]
# main algorithm: true-until-proven-false
distinct = True
for i in range(n):
    for j in range(i+1, n):
        if L[i] == L[j]:
            distinct = False
            break # from inner loop
    if not distinct:
        break # from outer loop

# display of result
if distinct:
    print("elements in L are distinct")
else:
    print("elements are not distinct") 
elements are not distinct

The in operator

To check if an element exists in a list, can use the regular looping mechanism to do so:

In [14]
x = int(input("enter an integer: "))
L = [1, 2, 5, -1, 0]
n = len(L)

found = False
for i in range(n):
    if L[i] == x:
        found = True
        break

if found:
    print(x, "is in the list", L)
else:
    print(x, "is not in the list", L)
enter an integer: 5
5 is in the list [1, 2, 5, -1, 0]

An alternative (and simpler) way, is the use of in operator. Note that it is equivalent to a for loop, since the code needs to check all elements to confirm if x is in L.

In [15]
x = int(input("enter an integer: "))
L = [1, 2, 5, -1, "CSC243"]

if x in L:
    print(x, "is in the list:", L)
else:
    print(x, "is not in the list:", L)
enter an integer: 3
3 is not in the list: [1, 2, 5, -1, 'CSC243']
Example: element distincness problem (revisited using slicing and in)
In [16]
# user-input
st = input("Enter integers separated by whitespaces: ")
L = st.split(" ")
n = len(L)
for i in range(n):
    L[i] = int(L[i])

# main work: true-until-false pattern
distinct = True
for i in range(n):
    if L[i] in L[i+1:n]:
        distinct = False
        break # from loop (controller by i)

# display of result
if distinct:
    print("elements in L are distinct")
else:
    print("elements are not distinct") 
Enter integers separated by whitespaces: 1 2 3 4
elements in L are distinct

Here is a version that relies on the while-loop to break automatically when distinct changes to False:

In [17]
# user-input
st = input("Enter integers separated by whitespaces: ")
L = st.split(" ")
n = len(L)
for i in range(n):
    L[i] = int(L[i])

# main work: true-until-false pattern
distinct = True
i = 0
while i < n and distinct:
    if L[i] in L[i+1:n]:
        distinct = False
    i += 1

# display of result
if distinct:
    print("elements in L are distinct")
else:
    print("elements are not distinct") 
Enter integers separated by whitespaces: 1 2 3 4
elements in L are distinct

list copying

To copy the contents of a list called L1 into another list, we must:

  1. Declare a new list of the same size and initialize it (example, call it L2).

  2. Loop over the contents of the original list L1.

  3. Set the first element of L2 equal to the first element of L1, the second element of L2 equal to the second in L1, and so on.

In [18]
st = input("Enter integers separated by whitespaces: ")
L1 = st.split(" ")

L2 = [0]*len(L1)
for i in range(len(L1)):
    L1[i] = int(L1[i])
    L2[i] = L1[i]
    
print("L1 is:", L1)
print("L2 is:", L2)
Enter integers separated by whitespaces: 1 2 3
L1 is: [1, 2, 3]
L2 is: [1, 2, 3]

Another, simpler way, is to use the .copy() method which is associated with the list object.

In [19]
L2 = L1.copy()

Note: the built-in id function

The id function returns a unique identifier for each object.

In [20]
truthValue = L1 == L2
print(truthValue)
print(id(L1))
print(id(L2))
True
4484194368
4484184384

Note how, while the two objects contain the same elements, each list is a unique object with its own identifier.

list aliasing

What if we write L2 = L1? This will result in aliasing. In fact, the assignment operator = will NOT perform a value copy (unlike for scalar types); L2 is simply another name (alias) for L1. What are the consequences for this? Changes in the values of L2 affect L1 as well, and vice-versa.

If we don’t want this, we use the .copy() or we process by looping, as done in the previous two methods.

In [21]
st = input("Enter integers separed by whitespaces: ")
L1 = st.split(" ")
for i in range(len(L1)):
    L1[i] = int(L1[i])
    
L2 = L1
print("L1 is (before assignment):", L1)
L2[0] = 'X'
print("L1 is now:", L1)
print("L2 is:", L2)
Enter integers separed by whitespaces: 1 2 3
L1 is (before assignment): [1, 2, 3]
L1 is now: ['X', 2, 3]
L2 is: ['X', 2, 3]

Example: checking if two lists are equal

This is another example of “True-until-proven-False” pattern. We initialize a bool to True. Then we check each element in the lists and compare them against each other to find a counter-example. This is the classical way.

In [22]
are_equal = True
for i in range(len(L1)):
    if L1[i] != L2[i]:
        are_equal = False
        break

if are_equal:
    print("lists are equal.")
else:
    print("lists NOT equal.")
lists are equal.

In Python, there exists a simpler way via the equality-check == operator.

In [23]
if L1 == L2:
    print("lists are equal.")
else:
    print("lists are NOT equal.")
lists are equal.

list concatenation

We can join two lists together using the + operator. This is similar to string concatenation, as we have seen before. We cannot concatenate a list with another data type, such as a str or an int. The += augmented assignment operator can also be used to concatenate lists.

Here is an example:

In [24]
L1 = [1,2,3,4]
L2 = [6,7,8,9]
L = L1 + L2
print(L)
[1, 2, 3, 4, 6, 7, 8, 9]

Note here how the + operator did not result in element-by-element addition. Instead, it added the one list to another.

Two-dimensional list

The list elements can be lists themselves. In this case, it is useful to think of the list as rows and columns. Processing the list in this scenario requires nested-loops and the use of two indices.

In [25]
L = [[1,2,3], [4,5,6], [7,8,9]]
print("first row:", L[0])
print("last row:", L[-1])
first row: [1, 2, 3]
last row: [7, 8, 9]
Example: printing a list in matrix form
In [26]
rows = len(L)
for i in range(rows):
    cols = len(L[i])
    for j in range(cols):
        print(L[i][j], end=" ")
    print()
1 2 3 
4 5 6 
7 8 9 

tuple data-structure

Use parentheses ( ) to initialize a tuple in Python. The elements are separated by commas, as in the case of list.

Examples:

In [27]
T1 = (1, 2, 3)
T2 = ("hello", 2.0, "CSC243", -1, True)

A tuple is either:

  1. Homogeneous: same data-type (like T1)

  2. Heterogeneous: mixed data-type (like T2)

Like in str and list, the built-in len() function gives the length of a tuple.

In [28]
print(len(T1))
print(len(T2))
3
5

Besides the method of initializaion, the difference is that tuples are immutable lists. Once initialized, they cannot be changed, i.e., we have read-only access. To read the i-th element, use the indexing operator [] as in lists.

In [29]
print(T2[0] + " " + T2[2])
hello CSC243

We can also compare tuples using the == operator.

In [30]
print("Are T1 and T2 equal?")
if T1 == T2:
    print("Yes!")
else:
    print("No.")
Are T1 and T2 equal?
No.

To initialize a tuple consisting of a single object x, use T = (x,) not T = (x). Example:

In [31]
x = "CSC243"
T = (x,)
print(T)
('CSC243',)

Quick tuple example

In [32]
location = (33.89311, 35.47782)

(lat, lon) = location
print(lat)
print(location[0])
print(lon)
print(location[1])
33.89311
33.89311
35.47782
35.47782
In [33]
T1 = (1,2,3)
(fd, sd, td) = T1

T2 = (4, 5, 6)
T = T1 + T2

print(T)
(1, 2, 3, 4, 5, 6)

list and tuple objects with or without mutable objects

In this section, we will examine what happens when a list or a tuple are themselves consisting of (im)mutable objects:

For a list which does not contain mutable objects, e.g., L = [1,20,10], the assignment operator L2 = L creates an alias, and L2 = L.copy() creates a clone.

In [34]
L = [1,20,10]
L2 = L

print("L's ID:", id(L))
print("L2's ID:", id(L2))

L2 = L.copy()
print("L2's ID:", id(L2))
L's ID: 4484363392
L2's ID: 4484363392
L2's ID: 4484363328

For a tuple which does not contain mutable objects, e.g., T = (1,20,10), we only need the assignment operator T2 = T since anyways we cannot change T (there is no copy() member function associated with the type tuple).

For a list containing mutable objects, e.g., L = [1, [3,6], 10], the assignment operator L2 = L creates an alias, and L2 = L.copy() creates a clone whose mutable objects are aliases. For instance, L2 using the .copy() method will create a new object, with its own unique identifier, but such that the mutable list [3,6] is aliasing the original one in L.

Example:

In [35]
L = [1, [3,6], 10]
L2 = L.copy()

L2[0] = "0"
print("L after modifying the first element of L2:", L)

L2[1][0] = "X"
print("L after modifying the mutable object in L2:", L)
print("L2 after both modifications:", L2)
L after modifying the first element of L2: [1, [3, 6], 10]
L after modifying the mutable object in L2: [1, ['X', 6], 10]
L2 after both modifications: ['0', ['X', 6], 10]

For a tuple containing mutable objects, e.g. T = (1,[3,6], 10) , T2 = T creates an alias.

Deep copy

What if we are interested in a deep copy of a list or tuple containing mutable objects? We need to clone all mutable and immutable objects all the way (i.e., recursively). We make use of the deepcopy() function from the copy module.

In [36]
from copy import deepcopy

L = [1, [3,6], 10]
L2 = deepcopy(L)
L2[0] = "0"
print("L after modifying the first element of L2:", L)
L2[1][0] = "X"
print("L after modifying the mutable object in L2:", L)
print("L2 after both modifications:", L2)
L after modifying the first element of L2: [1, [3, 6], 10]
L after modifying the mutable object in L2: [1, [3, 6], 10]
L2 after both modifications: ['0', ['X', 6], 10]

As you can see, modifying L2 in this case did not affect the original list L, thanks to deep copy.

list, string, and tuple as iterables and looping mechanisms

In Python, a list, str and tuple are all sequences. Hence, we can loop over them using cleaner syntax than the one using an index. The general syntax to do so is the following:

for variable in sequence: block of code

Example:

In [37]
L = [10, -8, 1, -2.2, 3.0] # L is a sequence

for i in range(len(L)):
    print(L[i], end=" ")

print("\n", end="")

for x in L:
    print(x, end=" ")
10 -8 1 -2.2 3.0 
10 -8 1 -2.2 3.0 
In [38]
my_string = "CSC 243"
for character in my_string:
    print(character, end="\n")
C
S
C
 
2
4
3

Note that in the above, the variable character is simply a placeholder for the element that loops over the list. It does not hold actual meaning and consequently we could have named it anything we like.

In [39]
my_string = "CSC 243"
for c in my_string:
    print(c)
C
S
C
 
2
4
3

list comprehension

In Python, we can initialize a list using the following syntax:

L = [function_of(x) for x in sequence]

This is called list comprehension. The sequence in the above is any iterable, such as a list, a str, or a tuple. Also, function_of(x) is an expression, which is a function of x, as the name indicates.

In [40]
import math

L1 = [math.sqrt(x) for x in range(1, 20)] # function_of(x) is sqrt(x)
print("L1 is:", L1)

print()
L2 = [i for i in range(10, 0, -2)] # function_of(x) is x
print("L2 is:", L2)

print()
L3 = [x*x for x in range(0, 5)] # function_of(x) is x**2
print("L3 is:", L3)
L1 is: [1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903, 3.0, 3.1622776601683795, 3.3166247903554, 3.4641016151377544, 3.605551275463989, 3.7416573867739413, 3.872983346207417, 4.0, 4.123105625617661, 4.242640687119285, 4.358898943540674]

L2 is: [10, 8, 6, 4, 2]

L3 is: [0, 1, 4, 9, 16]

Here is an example to achieve type-casting via list comprehension:

In [41]
L = input("Enter integers (separated by whitespaces): ").split(" ")
print(L)
L = [int(x) for x in L]
print(L)
Enter integers (separated by whitespaces): 4 8 15 16 23 42
['4', '8', '15', '16', '23', '42']
[4, 8, 15, 16, 23, 42]

Advanced list comprehension

A more advanced way to initialize a list is to add a condition for the previous syntax:

L = [function_of(x) for x in sequence if condition]

Example: filtering out the integers from a list with mixed elements (heterogeneous list).

In [42]
mixed_list = [1, "-2", 2, True, (18,25), False, "CSC243", -5]
print("mixed list:", mixed_list)
L = [x for x in mixed_list if type(x)==int]
print("L (filtered list):", L)
mixed list: [1, '-2', 2, True, (18, 25), False, 'CSC243', -5]
L (filtered list): [1, 2, -5]
In [43]