Python 8C - Dictionaries
Creating a Dictionary
Dictionaries are used in Python to link items of data together. The example on this page uses a footballer dictionary which links a player with a team they played for. To define a dictionary, use curly brackets { } and separate linked data with a colon.
​
A dictionary can be written on one line but the method below makes it easier to read:
Printing Data from a Dictionary
The first part of the linked data in a dictionary is called the key (e.g. each footballer in my example above).
​
The second part of the linked data in a dictionary is called the value (e.g. each team).
​
Example:
​
key : value
"Harry Kane" : "Tottenham Hotspur"
​
A for loop can be used to cycle through each set of keys and values in the dictionary:
Practice Task 1
a) Create a dictionary of your teachers and the subject they teach.
​
b) Print their name and the subject they teach on each line.
Example solution:
Adding and Removing Data from a Dictionary
Data can be added to a dictionary by stating the new key and value. You must use square brackets - [ ]
​
The new data will be added to the end of the dictionary. You can print the whole dictionary to see any changes - e.g. print(playerdictionary)
Data can be removed from a dictionary by stating the new key to remove in a pop command.
You can print the whole dictionary to see any changes - e.g. print(playerdictionary)
The whole dictionary can be cleared (reset to blank) using the clear command.
Practice Task 2
a) Ask the user to enter a new teacher and their subject.
​
b) Ask the user to remove a teacher.
​
c) Print the list of teachers and check the new teacher has been added and the other one removed.
Example solution:
Searching Through a Dictionary
An if statement can be used to check if a specific key is in a dictionary.
​
If the key is in the dictionary then a message can be displayed using the key and the value. Otherwise, an else statement can output an appropriate response.
To search for a value in a dictionary a for loop should be used to cycle through each key.
If the value of each key matches the value that is being searched for then it will be printed.
Practice Task 3
a) Create a search that allows a user to enter a teacher's name and prints the subject that they teach.
​
b) Include an else statement to print a response if a teacher is not in the dictionary.
Example solution:
Changing Data & Copying a Dictionary
The way to change values is similar to adding new data.
The first input below is to determine the key and the second input determines the new value to be changed to.
The copy command is used to make a duplicate of a dictionary.
Practice Task 4
a) Create a copy of your teacher dictionary.
​
b) Allow the user to enter a teacher and a new subject that they teach.
​
c) Print the copy of the dictionary with the new values.
Example solution:
Using a Dictionary to Make a Game
The code below is used to make a puzzle game where the user has to type in a footballer and the team they played for.
​
I have added comments to explain the different parts of the code.
​
A separate list has been created at the start to store the names of keys (players) that been correctly guessed.
​
A while loop is used to constantly ask the user for players and teams.
When they have guessed all 10 players (and the correct list reaches 10) the loop breaks and the game end.
Instead of a further practice task here, Task 6 of the Section 8 Practice tasks page challenges you to make a similar game using a dictionary.