Skip to content
Learnearn.uk » Python Unit Home » Dictionaries

Dictionaries

Introduction

What are dictionaries?

Dictionaries are data structures that store key:value pairs.

The key is the thing that you use to find an item in a dictionary.

The value is the item that is stored under that key.

When should I use them?

Dictionaries should be used where you need to store data in a data structure and access it by it’s name.

Dictionaries are un-ordered, so that order that you put an item in a dictionary is not necessarily the order that your item is stored in. Other other words if you need to maintain data order, use a list instead.

dictionary

Video Tutorial


Counter Example

Counter Example

Dictionaries are often used as counters to keep a tally of items. Here is a simple example to show you how it works.

More Examples

Challenge 29

Challenge 29 – Takeaway App

Create a program with a dictionary containing the names and telephone numbers of 5 takeaways.

 Bronze

  • Your program should ask the user to enter a takeaway name, and then print out the matching number.

 Silver

  • If the takeaway cannot be found the print out an appropriate message.

  Gold

At the start of the program it should print out the names of all the takeaways, so the user know which options they have to choose from.

30

Challenge 30 – Car counter

Create a program that a user can use to keep track of what make of cars pass by on a busy road.

 Bronze

  • The program should ask for 10 cars and should keep track of the number of cars of that make, using a dictionary.

 Silver

  • The program should be adapted so that cars will be keep being added until an empty string is entered, then it should print out the contents of the dictionary.

 Gold

  • The program should display the contents of the dictionary, sorted in frequency order (highest frequency first).

31

Challenge 31 – Telephone Contacts app

You should create an app that asks the user to enter some contact names and telephone numbers.

Your program should store each name and telephone number in a dictionary

 Bronze

  • The program should ask the user to enter 3 names / numbers.
  • The program should display all the names and numbers at the end of the program.

 Silver

  • The program should keep adding names / numbers until the user enters and empty string for a name, then it should stop.
  • The program should give the user the option to search for a contact’s telephone number.

 Gold

  • Your program should be adapted to have 4 screens:
    • A main menu
    • An add screen ( to add new contacts
    • A search screen (to search for contacts)
    • A view screen(to display all contacts)

32

Challenge 32 – Game Stats adder.

You should create a program that allows the user to add stats for a player. (e.g. strength 20, damage 30 , speed 50, etc)

Bronze

  • Your program should add all the stats from one character and then print out a full stat sheet at the end.

 Silver

  • Your program should include a list of valid stat categories and ensure that only valid categories are added.
  • Your program should check the amounts entered are valid integers between the range 0 to 100.

 Gold

  • Your program should be adapted so that stats for multiple players’s stats can be added.
  • Your program should give the user to option to select which player’s stat sheet should be printed out.