>>> days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
>>> print(days)
['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
>>> numbers = [1, 2, 3, 4, 5]
>>> print(numbers)
[1, 2, 3, 4, 5]
>>> stuff = [1, True, "xyzzy", 5.67]
>>> print(stuff)
[1, True, 'xyzzy', 5.67]
The items in a list are numbered starting with 0
The ith item in the list lst
is lst[i]
It is an error to try to retrieve an item by number if the number is too large
The number of an item is called its index.
>>> days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
>>> days[0]
'Sun'
>>> days[1]
'Mon'
>>> days[2]
'Tue'
>>> days[6]
'Sat'
>>> days[7]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
len
function gives the length of a list>>> days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
>>> len(days)
7
>>> empty = []
>>> len(empty)
0
You can change items in an existing lists
We say that lists are mutable (unlike strings)
>>> stuff = [1, True, "xyzzy", 5.67]
>>> stuff[0] = "foo"
>>> stuff[2] = 4
>>> stuff
['foo', True, 4, 5.67]
+
>>> list1 = [0, 1, 2, 3]
>>> list2 = [4, 5, 6, 7]
>>> list1 + list2
[0, 1, 2, 3, 4, 5, 6, 7]
>>> stuff = [1, True, "xyzzy", 5.67]
>>> stuff[-1]
5.67
>>> stuff[-2]
'xyzzy'
>>> stuff[-4]
1
>>> numbers = [5, 1, 3, 7, 4, 6, 2]
>>> max(numbers)
7
>>> min(numbers)
1
>>> sum(numbers)
28
>>> strings = ["foo", "xyzzy", "plugh"]
>>> max(strings)
'xyzzy'
>>> min(strings)
'foo'
>>> numbers = [1, 2, 3]
>>> numbers * 4
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
Sorting a list means to arrange the items in order
sorted(lst)
returns a new list that is sorted
sorted(lst)
does not alter lst
>>> numbers = [5, 1, 3, 7, 4, 6, 2]
>>> sorted(numbers)
[1, 2, 3, 4, 5, 6, 7]
>>> strings = ["foo", "xyzzy", "plugh"]
>>> sorted(strings)
['foo', 'plugh', 'xyzzy']
in
operator>>> numbers = [1, 2, 3]
>>> 1 in numbers
True
>>> 3 in numbers
True
>>> 4 in numbers
False
>>> days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
>>> days[1:3]
['Mon', 'Tue']
>>> days[4:]
['Thu', 'Fri', 'Sat']
>>> days[:5]
['Sun', 'Mon', 'Tue', 'Wed', 'Thu']
>>> days[:-3]
['Sun', 'Mon', 'Tue', 'Wed']
>>> days[-1:]
['Sat']
Removing items from a list
The del
operator removes one or more items
>>> days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
>>> del days[3]
>>> days
['Sun', 'Mon', 'Tue', 'Thu', 'Fri', 'Sat']
>>> del days[2:4]
>>> days
['Sun', 'Mon', 'Fri', 'Sat']
Aliasing of lists
List variables are references
Assignment makes one list variable point to another
After assignment, changing one list changes the other also
>>> days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
>>> d = days
>>> d[1] = "Monday"
>>> d
['Sun', 'Monday', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
>>> days
['Sun', 'Monday', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
When a function is called, the parameters are replaced by (copies of) the values of the actual arguments
A function cannot change a simple item passed as an argument because it operates on a copy
>>> def f(n):
... n = 3
...
>>> x = 1
>>> f(x)
>>> x
1
The value of a list variable is a reference to the list
A function can’t change a reference that’s passed as an argument, but it can the contents of the list to which the argument refers
>>> def f(lst):
... del lst[2]
... lst[0] = 5
...
>>> numbers = [1, 2, 3, 4, 5]
>>> f(numbers)
>>> numbers
[5, 2, 4, 5]