Oliver Irwin

16th March 2023


  • describe the concept of dictionnary
  • manipulate dicts to represent data
  • construct dicts from real-life situations


Say you are a teacher and you are grading students

You want to store the grades in a variable, but keep the information relating to the student available

students    = ["Alice", "Bob", "Charlie"]
first_test  = [12, 8, 15]
second_test = [17, 15, 20]

Another data type

The previous example shows how impractical using lists in this situation is

Python introduces another complex data type to represent structured data : dict

example = {"first": 12, "condition":False}

Dictionnary creation

In Python, dicts are created like variables

The data has to be enclosed by brackets {

The elements have to be separated by commas ,

The elements are key-value pairs


student = {
  "name": "Alice",
  "first_grade": 12,
  "second_grade": 17

Accessing an element

To access an element in a dict, you use its key

>>> print(student["name"])
>>> print(student["first_grade"])

Adding an element

You can add elements by adding a key

student["third_grade"] = 7

Useful functions

# Get all the keys in a dict
# Get only the values
# Get the number of keys
# Find out if a key is in a dict
key in d