top of page
top

Python 8C - Dictionaries

noun-list-1242568-FFFFFF.png

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:

py37.png

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:

py48.png
py47.png
py40.png
py39.png

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:

py49.png

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)

py43.png

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)

py44.png

The whole dictionary can be cleared (reset to blank) using the clear command.

py45.png

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:

py50.png

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.

py51.png
py52.png
py53.png

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. 

py54.png
py55.png

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:

py56.png

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. 

py58.png
py57.png

The copy command is used to make a duplicate of a dictionary.

py59.png

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:

py60.png

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.

py61.png
py62.png

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.

bottom of page