Ensure that you are familiar with lesson 9 and the text (ch8) for lists and also with the section on strings. Another useful resource is this page which includes some additional information and an example of encryption.
The list object in python is a collection that can store different object types. Indexing and iteration through a list works exactly the same as with strings.
Lists are a MUTABLE data structure.
The list builtin function can be applied to other object types:
>>> word = 'hello'
>>> print(list(word))
['h', 'e', 'l', 'l', 'o']
>>> my_tup = (200, 147, 255)
>>> list(my_tup)
[200, 147, 255]
Indexing in a list works just like with strings, but unlike strings which are composed of chars, a list may be composed of other iterables. This allows us to index into iterables within a list, and even into the iterables within the iterables within the list:
>>> my_list = [1, 5.2, 96, "FACE", "Python", 8.6, ['a', 'b', 3, 'c'], 333]
>>> my_list[-1]
333
>>> my_list[-2][0]
'a'
>>> my_list[3][-1]
'E'
>>> my_list[-2][-1] = 'cat' # modify a value => mutable
>>> my_list
[1, 5.2, 96, 'FACE', 'Python', 8.6, ['a', 'b', 3, 'cat'], 333]
>>> my_list[-2][-1][1]
'a'
>>> my_list[-20]
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
IndexError: list index out of range
a list of lists can be used to create a 3D type structure
In addition to the preceding parts :D
List support two operators: + for concatenation and * for repeating the elements.
>>> ls_1 = [1]
>>> ls_2 = [2, 3]
>>> ls_3 = [4, "5", (6, 7)]
>>> ls_1 + ls_2 + ls_3
[1, 2, 3, 4, '5', (6, 7)]
>>> lst_1 = [1, 2]
>>> lst_1 * 3
[1, 2, 1, 2, 1, 2]
We can slice a list to create another list from its elements.
>>> my_list = [1, 5.2, 96, "FACE", "Python", 8.6, ['a', 'b', 3, 'c'], 333]
>>> my_list[2 : 6]
[96, 'FACE', 'Python', 8.6]
We can iterate over list elements using the for loop.
We can use “in” operator to check if an item is present in the list or not. We can also use “not in” operator with a list.
A list is used to store homogeneous elements where we want to mutate - add/update/delete - elements.
Here is a list (pun) of some of the most commonly used list methods, and functions you will be familiar with such as the len function (works on all collections).
We can use a loop to read a list, or to write a list. In reading a list, the len of the list will be definite, so a for loop is an appropriate form. If creating a list of indefinite length, we may use a while loop, and append into the list until a condition triggers an end to the loop.
reading a list using a for loop with the range function
The code below will read a list of multiline str and loop through each one with a one second delay between images.
Images are ascii characters. The cls() function clears the screen between displaying each character.
While the above code uses a range in order to print the index as well as the value squared, we could omit the range if we did not want the index, or we could have used a while loop instead.
This may seem trivial, but is in fact very important to understand- when a list is passed to a function, the function receives a reference to the actual list- NOT A COPY.
str: passes to function by value (a copy)
By contrast:
list: passes to function by reference
This can be a source of huge confusion when working with lists, as one may think they are working with a copy but are in fact working with the original source list. Any mutating (append, pop, remove, delete, addition etc) that happens in the function (local scope) has an effect on the list in global scope.
The second time that the function is called, it receives a copy of the list, or perhaps more accurately, a slice of all the values that the list contains. When we print a slice of a list, does it change the list or make a copy of the contents to display for us? It is the same logic here- the slice takes a copy of everything in the list, sends it to the function, the function appends on values, and returns this mutated list. The mutated list is printed and then discarded as it is not assigned to any variable and is thus lost to be picked up by the garbage collection service.