Table of Contents
- Dicts and Lists, a crash course
- Exercises
Dicts and Lists, a crash course
This is a quick introduction to lists and dictionaries in python. If you are familiar with these python objects, please, help your fellow participants out, check out the exercises or skip to the next section or to a description of the DNA of a plot.ly graph
Dictionaries and lists are containers.
The most common kind of variable, or thing in python is either a number or string variable.
height = 180.31
name = 'Errol'
age = 30
gender = 'male'
As we often have many such variables, there fortunately exist varaibles that are capable of containing other variables within them. We can call these containers. Dictionaries and lists are containers.
Lists are ordered containers
Creating Lists
Lists are defined using square brackets: myList = []. This would be an empty list.
The elements of a list - that is, the other variables contained within it - are placed in the list by being between the square brackets and separated by a comma:
myDetails = [height, name, age, gender]
Ordering
Lists remember the order in which elements are put into them. So, when I call on the first element in the list, with the index zero, I will get my height, 180.31, back.
myDetails[0]
returns ...
180.31
If you were to go through the elements of the list in a for loop, you would go through them in this same order.
for detail in myDetails:
print(detail)
returns ...
180.31
Errol
30
male
Dictionaries are named containers
Names, not orders
Dictionaries contain other variables just like lists. But they don't care about or keep track of the order in which they are added.
Instead, each variable added to a dictionary must have a unique name, known as a key. That variable is then retrievable by using the name or key given to it. It is not retrievable by the order in which it was put into the dictionary.
Creating a dictionary
Dictionaries are defined by using curly brackets: myDict = {}.
The elements of a dictionary are given a key, that is a string, such as 'height', which is followed by a colon and then the variable that will be this particular element. Each element is separated by a comma as with lists.
So, the dictionary equivalent of the myDetails list above would be made as follows:
myDetailsDict = {'height':180.31, 'name':'Errol', 'age':30, 'gender':'male'}
A more user friendly way
However, making each key a string, and typing all of the quotation marks, can be cumbersome. An alternative that will be used in this course is to use the function dict(), which helps in making the creation of dictionaries easier.
When using the function dict(), the keys don't need to be strings, you can simply type in what you want the key to be and the function will convert it to a string automatically. Also, instead of using a colon, the equals sign is used.
myDetailsDict = dict(height=180.31, name='Errol', age=30, gender='male')
Both methods create the same thing. Generally though, using dict() is easier to identify amongst a block of code.
Accessing elements of a dictionary
Much like an element of a list is accessed by placing its position in square brackets after the name of the list (eg myList[0]), a dictionary element is placed by placing not its position but its key in the square brackets:
myDetailsDict['name']
returns ...
'Errol'
Looping Dictionaries
Introduction
Dictionaries are not ordered. And so looping through all of the elements of a dictionary is not usually the reason we use them, at least in dealing with plotly.
Usually, you will manipulate particular elements of a dictionary according to what its key is.
Nonetheless, looping can be done and can be useful or powerful.
Because each element in a dictionary is made up of two 'bits' — a key and the variable we've attached to it, there are more options for the way in which you loop through the dictionary.
Setting up a Dictionary for loops
Either way, though, the way you loop through a dictionary is by creating a list of all of the elements of the dictionary.
Soooo ... if you wanted to get all the keys of the dictionary, with out their corresponding variables, you would do the following:
myDetailsDict.keys()
returns ...
['gender', 'age', 'name', 'height']
To get all of the elements into a list, where the keys and their corresponding variables are paired together ...
myDetailsDict.items()
returns ...
[('gender', 'male'), ('age', 30), ('name', 'Errol'), ('height', 180.31)]
Using a Dictionary in a loop
Using conditional statements about the nature or content of a key or its corresponding variable, we can start to intelligently manipulate our dictionary.
Let's say that we want to get out of our dictionary only the variables that are strings. We would use the type() function, built into python, which returns what type of variable a variable is. For a string, this is str.
type('hello world')
returns ...
str
We can use this to filter out the elements of the dictionary that are strings.
# this will cycle through each key, encoded as 'k'
for k in myDetailsDict.keys():
# check if variable is a string
if type(myDetailsDict[k]) == str:
# if it's a string, print!
print myDetailsDict[k]
returns ...
male
Errol
Or, because python is awesome with strings, we could get out every variable that has a key that ends in 'e'.
We can do this by using the fact that strings in python are ordered and behave like lists when given indices in square brackets. Also very useful, is that negative indices start at the end, not the beginning.
text = 'python'
print([text[-1], text[-2], text[-3], text[-4], text[-5], text[-6]])
returns ...
['n', 'o', 'h', 't', 'y', 'p']
Soooo ... modifying our loop from above:
# this will cycle through each key, encoded as 'k'
for k in myDetailsDict.keys():
# check if last letter of key is 'e'
if k[-1] == 'e':
# if the key ends in 'e', print the key and its variable
print k, myDetailsDict[k]
returns ...
age 30
name Errol
What can containers contain
Containers, that is both lists and dictionaries, as you've seen from the examples above, can contain strings and numbers.
Very usefully, both lists and dictionaries can contain any kind of variable, including other containers. Containers can be nested in other containers.
A list can contain have lists as its elements:
data = [ # opens the main list
[1, 2, 3, 4, 5], # one to five
[1, 4, 9, 16, 5] # the squares of one to five
] # closes the main list
Indexing for the second element will return the second list ...
print(data[1])
returns ...
[1, 4, 9, 16, 5]
Dictionaries can have lists too. Generally, and especially with plot.ly, this is what is very useful about dictionaries: the ability to bundle up different sets of data into a single object, the dictionary, with each bit of the data having an informative and unique name.
dataSet = dict( # opens the dict function
# names of the team members
names = ['Errol', 'The Queen', 'Dan', 'Alistair', 'Nikki'],
# ages of the team members
ages = [30, 143, 18, 63, 12]
) # closes the dict function
... now we can string the data set however we want to ...
# print appropriate headings for the data we're about to spit out
print( 'Name, Age')
# we have five team members (this can be calculated as: len(dataSet['names']))
for i in range(5):
# for each team member, pull out the name from 'names' and the age from 'ages'
# notice how the use of keys makes it more obvious what's going on in the code
print( dataSet['names'][i], dataSet['ages'][i] )
returns ...
Name, Age
('Errol', 30)
('The Queen', 143)
('Dan', 18)
('Alistair', 63)
('Nikki', 12)
Exercises
- Make a list called
xthat contains a set of numbers of your own making- Make a similar list called
y.
- Make a similar list called
- Make a dictionary containing both of the above lists, with the names/keys
xdataandydata. - Presume that the
xdataandydataare paired, such that the first element of each form a pair, and the second element of each form another pair; ie[[x1,y1], [x2, y2], ...]. Now print out all of these pairs. - Now add another element to your dictionary. This element should be a string that gives your dictionary a title. Give it the name/key 'title'.
- Come up with new
xandylists. Now replace thexdataandydataelements of your dictionary with these new lists.