= [1,3,5,7]
my_list print( my_list )
[1, 3, 5, 7]
lists
and dicts
Data structures are collection types that group lots of data into a single object and make working with lots of data easier. Python has some built in data structures we can use. In this section we’ll look at using some of the most common and fundamental ones. As you gain exeperience with Python, you’ll find that many other more complex data structures are made out of these.
The simplest data structure is a list
. We can create a list simply by enclosing our data in square brackets.
= [1,3,5,7]
my_list print( my_list )
[1, 3, 5, 7]
More often, though, we’ll get a list
as the result of a function. Recall the random
library we used earlier, it can sample k
items from a list.
import random
= random.sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=3)
selected
print( selected )
['red', 'red', 'blue']
Here we get a list of integers. The point being it is now a collection of data that we can refer to as a single entity.
Lists can mix up any sort of data type,
= [1,2, 'three', 'IV', 5.0 ]
numbers_and_letters print( numbers_and_letters )
[1, 2, 'three', 'IV', 5.0]
including other lists.
= [ [1,2,3], ["a","b","c"] ]
list_of_lists
print( list_of_lists )
[[1, 2, 3], ['a', 'b', 'c']]
List elements can be accessed using indexing, like with strings.
print( numbers_and_letters[0] )
print( numbers_and_letters[2:4] )
1
['three', 'IV']
Indexing an element returns the whole element - so if that element happens to be a list itself - you get a whole list back
print( list_of_lists[1] )
['a', 'b', 'c']
To get at a single element in this case you must use multiple sets of square brackets, one for each list.
print( list_of_lists[0][1] )
print( list_of_lists[1][0] )
2
a
As they’re still a regular Python object, just like strings or they have lots of methods. Much of what you’ll want to do with a list is accomplished with methods. Again, there are many - here’s a list of useful ones https://www.w3schools.com/python/python_ref_list.asp. As a single example from the list, let’s look at .append()
, which is used to stick something on the right hand side of the list.
"ninety")
numbers_and_letters.append(print( numbers_and_letters )
[1, 2, 'three', 'IV', 5.0, 'ninety']
Another very common data structure is a dictionary. A dictionary is a data structure that has many unique keys
, each of which refers to a bit of other data called a value
. We can construct them using the curly brackets and the key/value
pairs
= {
my_dict "key1" : "value1",
"key2" : "value2"
}
print( my_dict )
{'key1': 'value1', 'key2': 'value2'}
Note the order in the dictionary isn’t preserved. The information is stored according to an internal algorithm, not the order the informaion is added. We can use the square brackets to get a single value, but as a dictionary has no order and therefore no numeric index, we must use the key as the index.
print( my_dict["key1"] )
value1
Dictionaries are useful when you want to ask for a bit of data by some name, rather than by its position in a list.
Dictionaries can hold anything in their values. But keys are restricted to particular datatypes. Strings and numbers are good keys, lists are not allowed.
print( { ["list_key", 1, 2] : ["some data"] } )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
As with lists there are methods on Python Dictionaries. Here’s a useful list https://www.w3schools.com/python/python_ref_dictionary.asp. These are mostly related to getting items on and off the list, we’ll see some when we use dictionaries in loops later.
= [["fluorine", "F"],
list_for_slicing "chlorine", "Cl"],
["bromine", "Br"],
["iodine", "I"],
["astatine", "At"]] [
Modify list_for_slicing
using the .reverse()
method. Verify that it is reversed as you expected.
Can you work out how to correct the wrong data in the dictionary below? Try to think of a way that doesn’t involve re-writing the whole dictionary. Hint: can you assign straight to a key?
= {
seasons 'spring' : ['mar', 'apr', 'may' ],
'autumn' : ['jun', 'jul', 'aug'],
'winter' : ['dec', 'jan', 'feb']
}