Lists Quick Review
List = A zero-indexed, ordered sequence of things that we can index into to pull out values.
We can create a list by putting a bunch of values in square brackets like so:
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
Zero-indexed means that our first value will be at index 0. To get a value from a list, we put the index in square brackets after the list name like so:
>>> lst[0] ⇨ 1
>>> lst[7] ⇨ 8
We can also do negative indexing, which will start at the back of the list
>>> lst[-1] ⇨ 8
>>> lst[-8] ⇨ 1
Helpful List-related Functions
range(a, b): Gives a list of numbers from a to b excluding b
range(1, 4) ⇨ [1, 2, 3]
len(lst): Gives the length of lst
len([1, 2, 3]) ⇨ 3
box and pointer diagrams
We can use what are called box and pointer diagrams to represent lists. A box and pointer diagram has a box for each index with the list’s value at that index inside of the box. If the element at a certain index is a list, we put an arrow inside that box and point it to the other list.
If we have lst1 = [1, 2, 3] and lst2 = [1, 2, [3, 4]] we can represent them as follows:
some things we can do with lists
Adding elements = Using the + operator, we can add a list onto the end of another list
Example:
lst1 = [1, 2, 3]
lst2 = lst1
lst1 = lst1 + [4]
I also included images of what the environment diagram looks like after line 2 and line 3 respectively. Note here that after line 3, lst2 is no longer pointing to the same list that lst1 is pointing to — the + operator creates a new list object and binds whatever is on the left side (in this case lst1) to the new list object.
List slicing = Creating a copy of a portion or all of a list.
List slicing is formatted with [start index (inclusive): end index (exclusive): skip], though you can choose to omit any of them if you’re using their default values (for start index, the default is 0, for end index, the default is the last index of the list, and for skip, the default is 0).
Example:
lst2 = [4, 5, 6, 7, 8]
lst2[1 : 5 : 2] ⇨ [5, 7]
This slicing will take from element 1 inclusive to element 5 exclusive with a skip of 2, which is a fancy way of saying that we’re copying every other element of the list [5, 6, 7, 8].
List comprehension = A compact, one line for loop that returns a list. List comprehensions are always formatted with [expression for variable name in list name if condition], though you can choose to omit the condition if you want to use all of the elements in your list. Another important thing to note is that this does not change your original list at all; it is creating a brand new list.
Example:
lst3 = [9, 10, 11, 12]
list_comp = [x + 1 for x in lst3 if x % 2 == 0]
>>> list_comp
[11, 13]
>>> lst3
[9, 10, 11, 12]
This list comprehension will go through all of the elements in lst3, and if an element is even, it will add 1 to the element and add it to the return list. And remember, lst3 will not be changed at all.
While slicing and list comprehensions are cool ways to create new lists, we can also mutate the list, or actually change the values in our original list.
List mutation Operations
lst.append(x): Adds a single element x onto the end of a lst and returns none -- if x is a list, the last element of lst becomes that list
lst1 = [2, 4, 6]
lst1.append(8)
>>> lst1
[2, 4, 6, 8]
lst1.append([10, 12])
>>> lst1
[2, 4, 6, 8, [10, 12]]
lst.extend(x): Adds a list onto the end of a list and returns none (in this case, x has to be a list, not just one element)
lst1 = [2, 4, 6]
lst1.extend(8)
Error
lst1.extend([10, 12])
>>> lst1
[2, 4, 6, 10, 12]
lst.pop(x): Removes and returns element at index x (or the last element if no index is specified)
lst1 = [2, 4, 6]
x = lst1.pop()
>>> x
6
>>> lst1
[2, 4]
lst.remove(x): Removes element x from lst and returns none (if x is not in the list, you will get an error)
lst1 = [2, 4, 6]
lst1.remove(4)
>>> lst1
[2, 6]
lst1.remove(3)
Error
lst.insert(i, x): Inserts x at index i and shifts the rest of the elements down one position
lst1 = [2, 4, 6]
lst1.insert(1, 3)
>>> lst1
[2, 3, 4, 6]
lst1 += lst2: Adds lst2 onto the end of lst1 (+= works exactly the same as extend)
lst1 = [2, 4, 6]
lst1 += 8
Error
lst1 += [10, 12]
>>> lst1
[2, 4, 6, 10, 12]
Here’s a helpful chart to remind you which operations are mutation operations and while operations create a new list.