Lists
List, Sets and Tuples¶
Lists¶
Lists are the python object that we can use to hold together a collection of multiple elements. They are declared with brackets and elements separated by commas. As well, like strings, they use indexing to pick out certain elements.
#Declare a list to hold the numbers 1, 2, and 3
l = [1, 2, 3]
print(l)
#Indexing for the first element returns 1
print(l[0])
#All three can be found with the indices 0, 1, and 2
print(l[0])
print(l[1])
print(l[2])
What happens if we look for an object at an index that does not exist? In this case we are going to get an error like the code below which illustrates how important it is to make sure you are deliberate with what you do.
#This will throw an error because there is no fourth element
print(l[3])
When it comes to avoiding this error, one thing to consider is checking the length of the list. Quite like a string, the len() function returns the number of elements in a list.
#len() returns to us the number of elements
print(len(l))
Using append for lists¶
All lists have the function append which means to add an element to the back of the list. There are two important things to know about this function.
- It will add whatever element is passed to the end of the list, increasing the length.
- It changes this list in place, what this means is that quite like when we use +=, the list is changed permanently.
#Append adds a value on to the end of a list
l.append(5)
print(l)
Unlike some other programming languages, lists in python do not require all elements to be of the same type. If we append a string to the list there will be no errors.
#It does not matter what the type is that we are adding
l.append('t')
print(l)
It is important that you do not try to append multiple items at once using append (we will show how to do it properly in a moment). The following will cause an error
#But appending two things at once is not allowed, this causes an error
l.append(5,9)
You might think that you can append a list and that will solve the issue but it actually will just end up nesting a list inside of a list. Because lists can take any types, you could even have the elements be more lists! Look at the effect of appending a list, now that latest element is also a list.
#The newest element is also a list, making a list within a list
l.append([5,9])
print(l)
#If we were to print that element we would get back a list
print(l[5])
If we have a list within a list, we can think about indexing twice to get an element within that nested list. To get the first element within the nested list (the last element of the master list), we would index twice with [4][0].
#For nested lists you are also able to index again to get an element within the indexed element
print(l[4][0])
Using extend for lists¶
In the case that we have multiple things to add to a list, extend will take a list as an argument and all those elements in order to the end of the list.
#Extend is the proper way to add elements in a list on to the end of another list
l = [1,2]
l.extend([5,9])
print(l)
We saw before how we add in place to an integer.
#Increment a by 1
a = 1
print(a)
a += 1
print(a)
This same logic can apply to a list, we can add a list to a list to extend it. The difference here is that we do NOT add the elements together mathematically but instead are adding the two lists together in terms of the number of elements.
#Add lists
l = [1,2]
l+=[5,9]
print(l)
Below we show that there are so many ways to add our elements to a list!
#Add elements to our lists in so many ways!
l = [1,2]
l.append(3)
l.extend([4,5])
l = l + [6]
l += [7]
print(l)
The other way that something could be added to a list is through using insert. The way it works is that you call insert with a list giving it the argument of the index to insert at, as well as the value to insert. Below, we show how to insert the value 3 at the index 2.
#Insert the value 3 at index 2
l = [1,2,4,5]
print(l)
l.insert(2,3)
print(l)
You are also able to change a value at an index (assuming it is a valid index).
#Change the value at index 2 to be 100
l[2] = 100
print(l)
Remove¶
The remove function gets rid of a given element. If we want to get rid of 100, we call remove 100.
#Remove 100 from the list
l.remove(100)
print(l)
An important feature of remove is that it will only remove the first version of an element, so one 100 is left in the list below.
l = [1, 2, 4, 5, 100, 100]
#Remove 100
l.remove(100)
print(l)
#Remove 100 again
l.remove(100)
print(l)