In this notebook we will learn about list and dictionary data structure, and separate code into functions.
Remember that you should think about what you expect the code to do before you run the cell. Then run the cell and see what happens. Then try changing the code slightly to make sure that you understand properly.
A list contains variables of any type. Use square brackets [ ] to indicate a list and a comma , to separate items in it. Lists can contain repetitions.
my_list = [1, 'a', 4.5, 1]
my_list
A list can even contain other lists.
alphabet_list = ['a', 'b', 'c']
numbers_list = [1, 2, 3]
my_list = [alphabet_list, numbers_list]
my_list
The function len()
returns the number of elements in a list, but it only counts top level elements -- if a member of the list is a list, that still only counts as 1 towards the length.
len(my_list)
Each member of a list is associated with a number that indicates its position in the list. The numbering starts from 0. The number associated with a list element is called its index. The next cell shows how to access the second element (i.e. element number 1) from list_of_names. This list only contains 3 elements -- what happens if you try to access element number 8?
Updating a list element is just like setting any othe variable.
list_of_names = ['Fred', 'George', 'Sam']
list_of_names[1]
list_of_names = ['Fred', 'George', 'Sam']
list_of_names[1] = 'Harry'
# show the updated list
list_of_names
Suppose you want to access the last element of a list, but you're not sure how many elements it contains. Then you can use negative numbers to count backwards through the list. Element -1 is the last element, -2 is second to last, etc.
list_of_names[-1]
To access part of a list -- not just a single element -- use list slicing. Instead of writing the index of one element to be returned, write the index of the first element that should be included, then a colon, then the index of the last element that should be included plus one.
list_of_friends = ['Ross', 'Joey', 'Chandler', 'Monica', 'Rachel', 'Phoebe', 'Gunther', 'Mike', 'David', 'Janice', 'Emily']
# start at index 3 (Monica), also include index 4 (Rachel) but *not* index 5.
# notice that 9-3=6 and 6 elements are included in the returned list.
list_of_friends[3:9]
If the index of the starting element is left out it's assumed to be 0. See what happens if you don't include an index for the ending.
list_of_friends[:4]
To access every other element also specify a step size as well as a start and end index. Try changing the step size.
# start at index 1 (Joey), then take every other element until reaching index 10 (Emily)
# notice (10 - 1) / 2 = 4.5, round up to 5, and there are 5 elements in the returned list
list_of_friends[1:10:2]
Elements can be added to the end of a list using the append()
function.
list_of_names = ['Fred', 'George', 'Sam']
# add 'Brandon' at the end of the list
list_of_names.append('Brandon')
list_of_names
To create an element at a specific index use insert()
.
Here we start with a list that has three elements and add a new element with index 1.
The later elements will have their indices updated.
What will happen if we try to insert the new element at position 7?
list_of_names = ['Fred', 'George', 'Sam']
# add 'Brandon' at index 1
list_of_names.insert(1,'Brandon')
list_of_names
Two lists can be joined together with the function extend()
.
list_of_names = ['Fred', 'George', 'Sam']
new_people = ['John', 'Harry', 'Steve']
# new_people list is added to the end of list_of_names (new_people list is unchanged)
list_of_names.extend(new_people)
list_of_names
It's also possible to use aritmetic-like symbols to modify lists.
+ is used to join two lists together, and * repeats a list.
The difference between using + or using extend()
is that + creates a new list and leaves both of the original lists unchanged.
alphabet_list = ['a', 'b', 'c']
numbers_list = [1, 2, 3]
new_list = alphabet_list + numbers_list
new_list
# this list has not been changed by the code in the previous cell
alphabet_list
alphabet_list * 3
The funtion pop()
returns the last element from the list and removes it from the list.
list_of_names = ['Fred', 'George', 'Sam']
# get the last element
list_of_names.pop()
# now list_of_names contains one fewer elements
list_of_names
If you know the value of the element you want to remove but not its index, use the function remove()
.
list_of_names = ['Fred', 'George', 'Sam']
list_of_names.remove('George')
list_of_names
If you know the index of the element you want to remove then use the keyword del
. Or use list slicing to delete a section of the list.
list_of_names = ['Fred', 'George', 'Sam']
del list_of_names[1:]
list_of_names
To remove all elements from a list use clear()
.
list_of_names = ['Fred', 'George', 'Sam']
list_of_names.clear()
# now the list contains no elements
list_of_names
To find out whether some value appears in a list use the keyword in
.
This will return a Boolean value (True
or False
).
list_of_names = ['Fred', 'George', 'Sam']
'Fred' in list_of_names
Find out how many times a value appears in your list with count()
and find out the position of the first occurrence of a value in the list with index()
.
list_of_names = ['Fred', 'George', 'Sam', 'Harry', 'George']
list_of_names.count('George')
list_of_names.index('George')
If we use index()
with a value that doesn't appear in the list at all this causes an error.
sort()
to sort the members of the listreverse()
to reverse the list sorted()
to returns a new list that's sorted, but leave the original list unchangedTry sorting lists containing elements of different types (integers, floats, strings, Booleans, other lists).
list_of_numbers = [5, 6.1, -2, 7, 1]
# sorts the list from lowest value to highest
list_of_numbers.sort()
list_of_numbers
list_of_numbers = [5, 6.1, -2, 7, 1]
# sorts the list from highest to lowest
list_of_numbers.sort(reverse = True)
list_of_numbers
list_of_numbers = [5, 6, -2, 7, 1]
list_of_numbers.reverse()
list_of_numbers
list_of_characters = ['x', '5', 'a', '!']
sorted_list_of_characters = sorted(list_of_characters)
sorted_list_of_characters
list_of_lists = [[4,2,3,], [2,-6,1], [2,9], [2,9,-1], [5]]
# compares the first element of each list, then the second...
list_of_lists.sort()
list_of_lists
list_of_lists = [[4,2,3], [2,-6,1], [2,9], [2,9,-1]]
mixed_list = ['b', 'a', 14, [1,2,3]]
# causes an error because some elements of different types aren't comparible
mixed_list.sort()
mixed_list
As with sorting, the functions max()
and min()
require that all the list elements can be compared to each other.
list_of_numbers = [5, 6.1, -2, 7, 1]
max(list_of_numbers)
min(list_of_numbers)
In this case all the elements must be numbers.
list_of_numbers = [5, 6.1, -2, 7, 1]
sum(list_of_numbers)
Here's a trick that might seem a bit strange but can be very useful: the * symbol before a list causes each of the members to be output. One place where this is used is if you have the values in a list that you want to use as the parameters for a function. Passing the list to the function doesn't work. Instead we want to pass in the members of the list, in order.
print([1,2,3])
print(*[1,2,3])
# causes an error
numbers = [1, 2, 3]
print('My favourite numbers are {}, {}, and {}'.format(numbers))
# this works
numbers = [1, 2, 3]
print('My favourite numbers are {}, {}, and {}'.format(*numbers))
Python dictionaries store a set of key:value pairs.
In a list we can store lots of values, and access a specific value by its position in the list (its index).
In a dictionary we access data by using its key.
The syntax to do this is dictionaryName[key]
.
The next cell gives an example of a dictionary in which the keys are British cities and the values are the populations, and shows how to access a value from the dictionary.
What happens if you try to access a value from the dictionary using a key that doesn't exist in it?
cityPopulations = {
'St Davids': 1841,
'St Asaph': 3355,
'City of London': 7375,
'Wells': 10536,
'Armagh': 14777,
'Ripon': 16702
}
# to access a value from a dictionary use dictionaryName[key]
populationOfWells = cityPopulations['Wells']
print('The population of Wells is', populationOfWells)
# the key could be stored in a variable.
# Change the value of this variable and see that the output of the next line changes.
cityName = 'City of London'
print('The population of {} is {}'.format(cityName, cityPopulations[cityName]))
An alternative way to access a dictionary item is to use get()
.
This will not cause an error if the key doesn't exist in the dictionary.
If the key doesn't exist then get()
will return None by default.
cityPopulations = {
'St Davids': 1841,
'St Asaph': 3355,
'City of London': 7375,
'Wells': 10536,
'Armagh': 14777,
'Ripon': 16702
}
print(cityPopulations.get('Wells'))
print(cityPopulations.get('Not a City'))
To add a key, value pair to a dictionary use update()
.
We can update with a single item or a list of key: value pairs.
cityPopulations = {
'St Davids': 1841,
'St Asaph': 3355,
'Ripon': 16702
}
# update with a single item
cityPopulations.update({"Truro": 18766})
print(cityPopulations)
cityPopulations.update({
'City of London': 7375,
'Wells': 10536,
'Armagh': 14777
})
print(cityPopulations)
And to remove an item from the dictionary use pop()
.
cityPopulations = {
'St Davids': 1841,
'St Asaph': 3355,
'City of London': 7375,
'Wells': 10536,
'Armagh': 14777,
'Ripon': 16702
}
# the pop() function returns the value of the item being removed
print(cityPopulations.pop('Wells'))
# now the item with key 'Wells' no longer appears in the dictionary
print(cityPopulations)
Remove all items of a dictionary with clear()
.
cityPopulations = {
'St Davids': 1841,
'St Asaph': 3355,
'City of London': 7375,
'Wells': 10536,
'Armagh': 14777,
'Ripon': 16702
}
cityPopulations.clear()
# now an empty dictionary
print(cityPopulations)
These three methods return all the keys used in a dictionary, all the values in the dictionary, or all the (key, value) pairs. They are incredibly useful if you want to iterate over a dictionary -- which we will be doing later in the course.
cityPopulations = {
'St Davids': 1841,
'St Asaph': 3355,
'City of London': 7375,
'Wells': 10536,
'Armagh': 14777,
'Ripon': 16702
}
print(cityPopulations.keys())
print(cityPopulations.values())
print(cityPopulations.items())