Python Dictionaries: How to Create and Use Them

Boot.dev Blog » Python » Python Dictionaries: How to Create and Use Them
Lane Wagner
Lane Wagner

Last published March 6, 2026

Table of Contents

Dictionaries are one of Python’s most useful data structures. Instead of accessing values by a numeric index like you do with lists, you access them by a key — usually a string. If lists are like numbered shelves, dictionaries are like labeled drawers.

All the content from our Boot.dev courses are available for free here on the blog. This one is the “Dictionaries” chapter of Learn to Code in Python. If you want to try the far more immersive version of the course, do check it out!

How Do You Create a Dictionary in Python?

Dictionaries are declared using curly braces with key: value pairs separated by commas:

car = {
    "brand": "Toyota",
    "model": "Camry",
    "year": 2019,
}

The keys here are the strings "brand", "model", and "year". The values are "Toyota", "Camry", and 2019. Keys must be unique — if you use the same key twice, the second value silently overwrites the first.

You can also start with an empty dictionary and populate it later:

planets = {}

How Do You Access Values in a Dictionary?

You retrieve a value by specifying its key in square brackets — similar to indexing a list, but with a key instead of a number:

car = {
    "make": "Toyota",
    "model": "Camry",
}
print(car["make"])  # Toyota

If you try to access a key that doesn’t exist, you’ll get a KeyError. To avoid that, you can check first with the in keyword:

cars = {
    "ford": "f150",
    "toyota": "camry",
}
print("ford" in cars)   # True
print("gmc" in cars)    # False

How Do You Add and Update Dictionary Values?

Setting a value uses the same bracket syntax with the assignment operator (=):

planets = {}
planets["Earth"] = True
planets["Pluto"] = False
print(planets)  # {'Earth': True, 'Pluto': False}

If the key already exists, its value gets overwritten:

planets = {"Pluto": True}
planets["Pluto"] = False
print(planets["Pluto"])  # False

This makes dictionaries great for building up data dynamically inside a loop. Here’s a common pattern — splitting full names into a dictionary of first → last:

full_names = ["jack bronson", "jill mcarty", "john denver"]
names_dict = {}
for full_name in full_names:
    parts = full_name.split()
    names_dict[parts[0]] = parts[1]

print(names_dict)
# {'jack': 'bronson', 'jill': 'mcarty', 'john': 'denver'}

How Do You Delete From a Dictionary?

Use the del keyword to remove a key-value pair:

names_dict = {
    "jack": "bronson",
    "jill": "mcarty",
    "joe": "denver",
}
del names_dict["joe"]
print(names_dict)  # {'jack': 'bronson', 'jill': 'mcarty'}

Be careful — if you try to delete a key that doesn’t exist, you’ll get a KeyError. Check with in first if you’re not sure.

How Do You Check if a Key Exists?

The in keyword checks for the presence of a key. This is especially useful when counting occurrences — a very common dictionary pattern:

def count_enemies(enemy_names):
    enemies = {}
    for name in enemy_names:
        if name in enemies:
            enemies[name] += 1
        else:
            enemies[name] = 1
    return enemies

If the key already exists, we increment its value with the += operator. If it doesn’t, we set it to 1. Without the in check, accessing a missing key would raise a KeyError.

How Do You Loop Over a Dictionary?

You can iterate over a dictionary’s keys using the same for loop syntax you’d use with a list. The loop variable gets each key, and you use bracket notation to get the corresponding value:

fruit_sizes = {
    "apple": "small",
    "banana": "large",
    "grape": "tiny",
}
for name in fruit_sizes:
    size = fruit_sizes[name]
    print(f"name: {name}, size: {size}")

# name: apple, size: small
# name: banana, size: large
# name: grape, size: tiny

This is the most common way to iterate over dictionaries when you need both keys and values.

What Are Nested Dictionaries?

Dictionaries can contain other dictionaries as values. This lets you represent structured, hierarchical data:

progress = {
    "character_name": "Kaladin",
    "quests": {
        "bridge_run": {
            "status": "In Progress",
        },
        "talk_to_syl": {
            "status": "Completed",
        },
    },
}

You access nested values by chaining bracket notation:

status = progress["quests"]["bridge_run"]["status"]
print(status)  # In Progress

Each bracket digs one level deeper into the structure. This pattern is very common when working with JSON data from APIs and web services.

What Should You Learn After Python Dictionaries?

You now know both of Python’s core data structures: lists and dictionaries. Together with loops and conditionals, you have the tools to build surprisingly powerful programs. From here, most beginners move on to functions in more depth, or start learning about sets and more advanced data structures.

If you want to keep going through the full Python curriculum with hands-on exercises, check out the Learn to Code in Python course on Boot.dev.

Frequently Asked Questions

Are Python dictionaries ordered?

As of Python 3.7, dictionaries maintain insertion order. In Python 3.6 and earlier they were unordered. If you are on 3.7 or later you can iterate over a dictionary and get the same order every time.


What happens if you access a key that does not exist?

You get a KeyError. To avoid this, check if the key exists first using the in keyword, or use the .get() method which returns None instead of raising an error.


Can dictionary keys be duplicated in Python?

No. Dictionary keys must be unique. If you use the same key twice when creating a dictionary, the second value silently overwrites the first.


What is the difference between a list and a dictionary in Python?

A list stores items in order and you access them by numeric index. A dictionary stores key-value pairs and you access values by their key, which can be a string, number, or other immutable type. Use a list for ordered collections and a dictionary when you need to look things up by name.