5.1 Lists and Tuples: Alternatives to Arrays
105
The elements of the list x:
hello
4
3.14
6
List Comprehension There is a special construct in Python that allows you to
run through all elements of a list, do the same operation on each, and store the
new elements in another list. It is referred to as list comprehension and may be
demonstrated as follows:
In [1]: L1 = [1, 2, 3, 4]
In [2]: L2 = [e*10 for e in L1]
In [3]: L2
Out[3]: [10, 20, 30, 40]
So, we get a new list by the name L2, with the elements 10, 20, 30 and 40, in that
order. Notice the syntax within the brackets for L2, e*10 for e in L1 signals that
e is to successively be each of the list elements in L1, and for each e, create the next
element in L2 by doing e*10. More generally, the syntax may be written as
L2 = [E(e) for e in L1]
where E(e) means some expression involving e.
In some cases, it is required to run through 2 (or more) lists at the same time.
Python has a handy function called zip for this purpose. An example of how to use
zip is provided in Sect. 5.5 (file_handling.py).
Some Properties of Tuples We should also briefly mention about tuples, which are
very much like lists, the main difference being that tuples cannot be changed. To a
freshman, it may seem strange that such “constant lists” could ever be preferable
over lists. However, the property of being constant is a good safeguard against
unintentional changes. Also, it is quicker for Python to handle data in a tuple than
in a list, which contributes to faster code. With the data from above, we may create
a tuple and print the content by writing
x = (’hello’, 4, 3.14, 6)
print(’The elements of the tuple x:\n’)
for e in x:
print(e)
Trying insert or append for the tuple gives an error message (because it cannot
be changed), stating that the tuple object has no such attribute.
105
The elements of the list x:
hello
4
3.14
6
List Comprehension There is a special construct in Python that allows you to
run through all elements of a list, do the same operation on each, and store the
new elements in another list. It is referred to as list comprehension and may be
demonstrated as follows:
In [1]: L1 = [1, 2, 3, 4]
In [2]: L2 = [e*10 for e in L1]
In [3]: L2
Out[3]: [10, 20, 30, 40]
So, we get a new list by the name L2, with the elements 10, 20, 30 and 40, in that
order. Notice the syntax within the brackets for L2, e*10 for e in L1 signals that
e is to successively be each of the list elements in L1, and for each e, create the next
element in L2 by doing e*10. More generally, the syntax may be written as
L2 = [E(e) for e in L1]
where E(e) means some expression involving e.
In some cases, it is required to run through 2 (or more) lists at the same time.
Python has a handy function called zip for this purpose. An example of how to use
zip is provided in Sect. 5.5 (file_handling.py).
Some Properties of Tuples We should also briefly mention about tuples, which are
very much like lists, the main difference being that tuples cannot be changed. To a
freshman, it may seem strange that such “constant lists” could ever be preferable
over lists. However, the property of being constant is a good safeguard against
unintentional changes. Also, it is quicker for Python to handle data in a tuple than
in a list, which contributes to faster code. With the data from above, we may create
a tuple and print the content by writing
x = (’hello’, 4, 3.14, 6)
print(’The elements of the tuple x:\n’)
for e in x:
print(e)
Trying insert or append for the tuple gives an error message (because it cannot
be changed), stating that the tuple object has no such attribute.
