Lesson 28 of 60
โฎPrev Nextโฏ

PY-101 ยท Collections

Dictionaries

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

28of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    A dictionary stores values under unique keys, written as {"name": "Asha", "age": 29}. You read a value with its key, d["name"], and add or update with d[key] = value. It is the right tool whenever you look things up by a name instead of a position.

    • Create and read a dict
    • Add and update keys
    • Use get() and items()

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    Dictionaries, running
    1. stock = {"tea": 12, "milk": 5}
    2. stock["bread"] = 8
    3. stock["milk"] = stock["milk"] - 2
    4. print(stock)
    5. print("Coffee:", stock.get("coffee", 0))
    6. for item, qty in stock.items():
    7. print(item, qty)
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemA phone book: you never ask for "the 427th entry". You ask for "Ravi" and get his number. That is exactly what a dictionary does.
    2. The Python idead = {key: value}A dictionary stores values under unique keys, written as {"name": "Asha", "age": 29}.
    3. In real lifeEvery web API response you read in Python (weather, payments, maps) arrives as JSON and becomes a dictionary.

    Start with a question

    A phone book: you never ask for "the 427th entry". You ask for "Ravi" and get his number. That is exactly what a dictionary does.

    JSON from APIs, settings, user profiles and counters are all dictionaries. Real Python code is full of them.

    Explain it like I'm 10

    A dictionary is a set of labelled drawers. To find something you read the label, not count the drawers.

    The technical explanation

    Keys are usually strings or numbers and must be unique. Values can be anything, even lists or other dicts.

    d[key] = value adds a new key or replaces the old value.

    for key, value in d.items(): loops over both. d.keys() and d.values() give each side.

    Syntax

    d = {key: value}
    d[key] = value
    d.get(key, default)

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Every web API response you read in Python (weather, payments, maps) arrives as JSON and becomes a dictionary.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. d = {"a": 1}. What is d["a"]?
    2. 2. What does d.get("z", 0) return when "z" is missing?
    3. 3. Can a dictionary have two identical keys?
    4. 4. Which loops over keys and values together?
    5. 5. d = {}; d["x"] = 5. What is d?

    Questions people ask

    Are Python dictionaries ordered?

    Yes. Since Python 3.7 dictionaries keep the order in which keys were added.

    How do I check if a key exists in a dictionary?

    Use if key in d:. It checks keys, not values.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner Are Python dictionaries ordered?

    Yes. Since Python 3.7 dictionaries keep the order in which keys were added.

    beginner How do I check if a key exists in a dictionary?

    Use if key in d:. It checks keys, not values.

    intermediate d = {"a": 1}. What is d["a"]?

    1. The key "a" holds 1.

    intermediate What does d.get("z", 0) return when "z" is missing?

    0. get returns the default.

    Quick revision

    Remember

    • Create and read a dict
    • Add and update keys
    • Use get() and items()
    • d["missing"] raises KeyError. d.get("missing", 0) returns 0 instead, which is perfect for counting.

    Syntax

    d = {key: value}
    d[key] = value
    d.get(key, default)

    Official documentation