Lecture On Dictionaries - Jupyter Notebook
Lecture On Dictionaries - Jupyter Notebook
Dictionaries do not store their information in any particular order, so you may not get your information back in the same order you entered
it.
General Syntax
A general dictionary in Python looks something like this:
Since the keys and values in dictionaries can be long, we often write just one key-value pair on a line. You might see dictionaries that look
more like this:
In [ ]: g={'a':'dog','b':'cat'}
print(g.values())
print(g.keys())
localhost:8888/notebooks/Desktop/Python Programming Course/jup notebooks for lectures/Python lectures 2021/Lecture on Dictionaries.ipynb# 1/13
1/23/2021 Lecture on Dictionaries - Jupyter Notebook
Example
A simple example involves modeling an actual dictionary.
In [ ]: python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
We can get individual items out of the dictionary, by giving the dictionary's name, and the key in square brackets:
In [ ]: python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
In [ ]: python_words.keys()
In [ ]: python_words.values()
In [ ]: python_words.items()
localhost:8888/notebooks/Desktop/Python Programming Course/jup notebooks for lectures/Python lectures 2021/Lecture on Dictionaries.ipynb# 2/13
1/23/2021 Lecture on Dictionaries - Jupyter Notebook
In [ ]: python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
The output is identical, but we did it in 3 lines instead of 6. If we had 100 terms in our dictionary, we would still be able to print them out
with just 3 lines.
The only tricky part about using for loops with dictionaries is figuring out what to call those first two variables. The general syntax for this
for loop is:
localhost:8888/notebooks/Desktop/Python Programming Course/jup notebooks for lectures/Python lectures 2021/Lecture on Dictionaries.ipynb# 3/13
1/23/2021 Lecture on Dictionaries - Jupyter Notebook
In [ ]: python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
print(python_words['dictionary'])
print(python_words['dictionary'])
localhost:8888/notebooks/Desktop/Python Programming Course/jup notebooks for lectures/Python lectures 2021/Lecture on Dictionaries.ipynb# 4/13
1/23/2021 Lecture on Dictionaries - Jupyter Notebook
In [ ]: python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
#del python_words['list']
If you were going to work with this code, you would certainly want to put the code for displaying the dictionary into a function. Let's see
what this looks like:
In [ ]: def show_words_meanings(python_words):
# This function takes in a dictionary of python words and meanings,
# and prints out each word with its meaning.
print("\n\nThese are the Python words I know:")
for word, meaning in python_words.items():
print("\nWord: " , word)
print("Meaning: " , meaning)
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
show_words_meanings(python_words)
show_words_meanings(python_words)
As long as we have a nice clean function to work with, let's clean up our output a little:
localhost:8888/notebooks/Desktop/Python Programming Course/jup notebooks for lectures/Python lectures 2021/Lecture on Dictionaries.ipynb# 5/13
1/23/2021 Lecture on Dictionaries - Jupyter Notebook
In [ ]: def show_words_meanings(python_words):
# This function takes in a dictionary of python words and meanings,
# and prints out each word with its meaning.
for word, meaning in python_words.items():
print(word, meaning)
python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
show_words_meanings(python_words)
show_words_meanings(python_words)
Make a new key, and copy the value to the new key.
Delete the old key, which also deletes the old value.
Here's what this looks like. We will use a dictionary with just one key-value pair, to keep things simple.
localhost:8888/notebooks/Desktop/Python Programming Course/jup notebooks for lectures/Python lectures 2021/Lecture on Dictionaries.ipynb# 6/13
1/23/2021 Lecture on Dictionaries - Jupyter Notebook
This works because the method .items() pulls all key-value pairs from a dictionary into a list of tuples:
localhost:8888/notebooks/Desktop/Python Programming Course/jup notebooks for lectures/Python lectures 2021/Lecture on Dictionaries.ipynb# 7/13
1/23/2021 Lecture on Dictionaries - Jupyter Notebook
This is actually the default behavior of looping through the dictionary itself. So you can leave out the .keys() part, and get the exact
same behavior:
In [ ]: python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
We can extend this slightly to make a program that lets you look up words. We first let the user choose a word. When the user has
chosen a word, we get the meaning for that word, and display it:
This allows the user to select one word that has been defined. If we enclose the input part of the program in a while loop, the user can
localhost:8888/notebooks/Desktop/Python Programming Course/jup notebooks for lectures/Python lectures 2021/Lecture on Dictionaries.ipynb# 8/13
1/23/2021 Lecture on Dictionaries - Jupyter Notebook
This allows the user to ask for as many meanings as they want, but it takes the word "quit" as a requested word. Let's add an elif
clause to clean up this behavior:
top
Let's take a look at the order that results from a simple call to dictionary.keys():
In [ ]: python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
The resulting list is not in order. The list of keys can be put in order by passing the list into the sorted() function, in the line that initiates the
localhost:8888/notebooks/Desktop/Python Programming Course/jup notebooks for lectures/Python lectures 2021/Lecture on Dictionaries.ipynb# 9/13
1/23/2021 Lecture on Dictionaries - Jupyter Notebook
e esu t g st s ot o de e st o eys ca be put o de by pass g t e st to t e so ted() u ct o , t e e t at t ates t e
for loop:
In [ ]: python_words = {'list': 'A collection of values that are not connected, but have an order.',
'dictionary': 'A collection of key-value pairs.',
'function': 'A named set of instructions that defines a set of actions in Python.',
}
Nesting
Nesting is one of the most powerful concepts we have come to so far. Nesting involves putting a list or dictionary inside another list or
dictionary. We will look at two examples here, lists inside of a dictionary and dictionaries inside of a dictionary. With nesting, the kind of
information we can model in our programs is expanded greatly.
Lists in a dictionary
A dictionary connects two pieces of information. Those two pieces of information can be any kind of data structure in Python. Let's keep
using strings for our keys, but let's try giving a list as a value.
The first example will involve storing a number of people's favorite numbers. The keys consist of people's names, and the values are lists
of each person's favorite numbers. In this first example, we will access each person's list one at a time.
localhost:8888/notebooks/Desktop/Python Programming Course/jup notebooks for lectures/Python lectures 2021/Lecture on Dictionaries.ipynb# 10/13
1/23/2021 Lecture on Dictionaries - Jupyter Notebook
Dictionaries in a dictionary
The most powerful nesting concept we will cover right now is nesting a dictionary inside of a dictionary.
To demonstrate this, let's make a dictionary of pets, with some information about each pet. The keys for this dictionary will consist of the
pet's name. The values will include information such as the kind of animal, the owner, and whether the pet has been vaccinated.
localhost:8888/notebooks/Desktop/Python Programming Course/jup notebooks for lectures/Python lectures 2021/Lecture on Dictionaries.ipynb# 11/13
1/23/2021 Lecture on Dictionaries - Jupyter Notebook
Clearly this is some repetitive code, but it shows exactly how we access information in a nested dictionary. In the first set of print
statements, we use the name 'willie' to unlock the 'kind' of animal he is, the 'owner' he has, and whether or not he is 'vaccinated'. We have
to wrap the vaccination value in the str function so that Python knows we want the words 'True' and 'False', not the values True and
False . We then do the same thing for each animal.
Let's rewrite this program, using a for loop to go through the dictionary's keys:
localhost:8888/notebooks/Desktop/Python Programming Course/jup notebooks for lectures/Python lectures 2021/Lecture on Dictionaries.ipynb# 12/13
1/23/2021 Lecture on Dictionaries - Jupyter Notebook
In [ ]:
localhost:8888/notebooks/Desktop/Python Programming Course/jup notebooks for lectures/Python lectures 2021/Lecture on Dictionaries.ipynb# 13/13