Python Dictionaries

picture of python with python logo in corner

Python dictionaries contain key:value pairs.

An empty dictionary can be created with curly braces ({})

  • my_dictionary = {}

You may also have data to include in your dictionary when you write your program:

pet_info = {
    "name":"Fido",
    "age":2,
    "breed":"mutt"
}

To access data in a dictionary, you call the dictionary name with the key in square brackets:

  • pet_info[“name”]

You can add a new key:value pair easily:

  • pet_info[“shots”] = True

You an also change the value by:

  • pet_info[“name”] = “Spot”

Or delete a key:value pair:

  • del(pet_info[“breed”])

Lists are a better way to store sequential data, but python dictionaries are better for creating lookup tables.