Lists and tuples
Contents
Lists and tuples#
Variables can also contain multiple entries of values. We call those lists and tuples. Some programmers also call them vectors or arrays; arrays of values. We already know one kind of array, strings. Strings are lists of characters.
See also
You can access elements in an array using square brackets []
which allow you access an element at a given index. Indexing starts at 0. Thus, the first element of an array is element number 0. The following string contains 5 characters and thus, element with index 0, 1, 2, 3 and 4 can be accessed:
word = "Hello"
word[0]
'H'
word[1]
'e'
word[2]
'l'
word[3]
'l'
word[4]
'o'
When accessing an index that is not in the list, we receive an error:
word[5]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Input In [8], in <module>
----> 1 word[5]
IndexError: string index out of range
Numeric lists#
Another type of array are numeric lists. They are common to store measurements of experiments for example:
measurements = [5.5, 6.3, 7.2, 8.0, 8.8]
measurements[0]
5.5
measurements[1]
6.3
Changing entries in lists works like this:
measurements[1] = 25
measurements[1]
25
You can also append entries to lists:
measurements.append(10.2)
Lists can also be reversed:
measurements
[5.5, 25, 7.2, 8.0, 8.8, 10.2]
measurements.reverse()
measurements
[10.2, 8.8, 8.0, 7.2, 25, 5.5]
Just like strings, you can also concatenate arrays:
more_measurements = [12.3, 14.5, 28.3]
measurements + more_measurements
[10.2, 8.8, 8.0, 7.2, 25, 5.5, 12.3, 14.5, 28.3]
When working with numeric lists, you can use some of python’s built-in functions to do basic statistics on your measurements
# minimum value in the list
min(measurements)
5.5
# maximum value in the list
max(measurements)
25
# sum of all elements in the list
sum(measurements)
64.7
# number of elements in the list
len(measurements)
6
# average of all elements in the list
sum(measurements) / len(measurements)
10.783333333333333
Mixed type lists#
You can also store values of different types in a list
mixed_list = [22, 5.6, "Cat", 'Dog']
mixed_list[0]
22
mixed_list[3]
'Dog'
type(mixed_list[3])
str
Tuples#
Tuples are lists which cannot be changed:
immutable = (4, 3, 7.8)
immutable[1]
3
immutable[1] = 5
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [31], in <module>
----> 1 immutable[1] = 5
TypeError: 'tuple' object does not support item assignment
You can convert tubles to lists and lists to tuples:
type(immutable)
tuple
mutable = list(immutable)
type(mutable)
list
again_immuntable = tuple(mutable)
type(again_immuntable)
tuple
Exercise#
Assume you did measurements on multiple days. Please compute average measurement of this week.
measurements_monday = [2.3, 3.1, 5.6]
measurements_tuesday = [1.8, 7.0]
measurements_wednesday = [4.5, 1.5, 6.4, 3.2]
measurements_thursday = [1.9, 2.0]
measurements_friday = [4.4]