Dictionaries: Beginner
Reddit stores each post's metadata - vote count, comment count, author, subreddit, awards, and flair - in a dictionary that travels from the database through the API to the browser, making Python's dict the universal data format gluing together every layer of a modern web application. Every JSON response from an API, every database record mapped into Python, and every configuration file loaded at startup becomes a dictionary the moment it enters your program. The dictionary skills you are about to learn are the most-used data structure in all of Python.
What is a Dictionary?
Look up data by name, not position
The Real Dictionary Analogy
> Access the "name" and "email" values from this dictionary. Remember: dictionaries use string keys, not numeric indices like lists.
user = { "name": "Alice", "email": "a@b.com" } print(user[]) print(user[])
Key-based access is what makes dictionaries so readable in practice. When you see user["name"] in production code six months after writing it, the meaning is obvious. With position-based access, you would need to look up which index is which.
Creating Dictionaries
Build key-value data structures
Syntax Breakdown
Empty Dictionary
> Create an empty dictionary and use the correct function to check the type of config. Remember: dictionaries use curly braces, not brackets or parentheses.
config = { "host": "localhost", "port": 5432 } empty = {} print((config)) print(len(empty))
Accessing Values
Retrieve values safely from any dict
Handling Missing Keys
- Use dict[key] when you know the key exists
- Use .get(key) when the key might be missing
- Check with "key in dict" before accessing
- Access a key without checking if it exists
- Ignore KeyError - it signals a real bug
- Assume all keys are present in user data
Safe Access with .get()
What if you're not sure whether a key exists? Python provides the .get() method, which returns None (or a default value you specify) instead of raising an error:
> Look up the "city" key, which does not exist in this dictionary. Use the method that returns a default value instead of crashing.
user = {"name": "Alice", "age": 28} city = user.("city", ) print(city)
Now try the interactive version below. Pick the right access method for each blank and run the code to see how bracket notation and .get() behave differently with missing keys.
> A user dictionary contains only "name" and "age" keys, and you need to look up "city" which does not exist. Pick the access method that handles the missing key gracefully.
user = {"name": "Alice", "age": 28} result = print(result)
Choosing between bracket access and .get() is a design decision about how your code should behave when data is missing. Bracket notation makes missing keys loud errors; .get() makes them silent defaults.
In production systems, .get() with a meaningful default is usually preferable. It keeps your pipeline running even when individual records have missing fields, and you can log or flag the missing data separately.
The in operator lets you check for key existence before access, which is another safe pattern. Use key in dict before bracket access when you want to take different actions depending on whether the key is present.
Adding and Updating Values
Modify dict entries on the fly
The Typo Trap
> Add an "email" entry to the user dictionary and update the "name" value. Then check how many entries the dictionary contains.
user = {"name": "Alice"} user[] = "alice@email.com" user["name"] = "Bob" print((user))
The fact that adding and updating use identical syntax is both a strength and a trap. It means you can always write dict[key] = value without checking first, but it also means typos silently create new keys rather than raising errors.
When you need to add multiple entries at once, the .update() method is more efficient than multiple individual assignments. Pass it another dictionary and all its entries will be merged into yours in a single operation.
Removing Items
Remove entries without crashing
The del Statement
The del keyword removes a key-value pair from a dictionary. If the key doesn't exist, it raises a KeyError:
The .pop() Method
The .pop() method removes a key and returns its value. This is useful when you need to both remove and use the value. You can also provide a default value to avoid errors if the key doesn't exist:
- Just removes the entry
- Raises KeyError if missing
- Returns nothing (None)
- More concise for simple removal
- Removes AND returns value
- Can specify default value
- More flexible and safe
- Use when you need the value
> Remove the "city" entry from the dictionary and capture the removed value. Then check how many entries remain.
user = { "name": "Alice", "age": 28, "city": "Seattle" } removed = user.("city") print(removed) print((user))
Clearing a Dictionary
To remove ALL items from a dictionary, use the .clear() method. This leaves you with an empty dictionary:
deldict[key]: removes the pair, raisesKeyErrorif missing.pop(key): removes and returns the value.pop(key, default): returns default if key is missing.clear(): removes all entries, leaves empty dict
> You want to remove the "city" key from a user dictionary, but it might not be present. Pick the removal method that won't crash if the key is missing.
user = {"name": "Alice", "age": 28} result = print(result) print(user)
Removal operations are important for keeping dictionaries lean. In data pipelines, you often want to strip internal metadata keys before forwarding a record downstream, and .pop() lets you do that while capturing the removed value if needed.
Use .clear() when you want to reuse the same dictionary object but start fresh. This is more efficient than creating a new empty dictionary because existing references to the variable remain valid.
Always think about whether a key is guaranteed to exist before choosing between del, .pop(), and .pop(key, default). Defensive use of .pop() with a default prevents unexpected crashes from optional fields.
Checking for Keys
Test key existence before access
Before accessing a key, you often want to check if it exists. The in operator checks whether a key is present in the dictionary:
This is the standard pattern for safely handling optional data. Check first with in, then access. Alternatively, use .get() with a default value as shown earlier.
Checking for Values
The in operator checks keys by default. To check if a VALUE exists in the dictionary, you need to explicitly use .values():
Dictionary Size
Just like with lists and strings, you can use len() to get the number of key-value pairs in a dictionary:
What Can Be a Key?
Choose valid key types confidently
- Strings: "name", "email" (most common)
- Numbers: 1, 42, 3.14
- Tuples: (1, 2) with immutable contents
- Booleans: True, False
- Lists: [1, 2, 3] are mutable
- Dictionaries: {"a": 1} are mutable
- Sets: {1, 2, 3} are mutable
- Any other changeable type
Keys Must Be Unique
Real-World Use Cases
Apply dicts to configs and counting
Configuration Settings
API Responses
Counting Occurrences
Common Mistakes to Avoid
Mistake 1: {} vs []
Mistake 2: = Instead of :
Mistake 3: Quotes on Keys
> This dictionary definition uses an equals sign instead of a colon for the first key-value pair, causing a SyntaxError.
SyntaxError: invalid syntax (expected : not =)
As your data engineering skills grow, you will layer these basics with more advanced techniques like defaultdict for auto-initializing keys, Counter for frequency analysis, and dict comprehensions for concise transformations.
> You are a data analyst at Stripe building a Python script that maps raw API field names to human-readable report headers, safely retrieves each mapping, updates labels when the API changes, removes deprecated fields, and checks membership before applying any rename transformation.
{} dict creation pairs each raw API field name with its display label in a structure that allows instant key-based lookup without scanning the whole mapping..get() safely retrieves each label without crashing when an unexpected API field name appears in the source data.dict[key] = value refreshes a display label in place whenever the API renames an existing field.in confirms a field exists in the mapping before attempting the rename, preventing KeyError at runtime.{} and colons : to create dictionariesdict[key] or safely with .get(key, default)dict[key] = value; remove with del or .pop()inKey-value pairs power real-world data
- Category
- Python
- Difficulty
- beginner
- Duration
- 34 minutes
- Challenges
- 3 hands-on challenges
Topics covered: What is a Dictionary?, Creating Dictionaries, Accessing Values, Adding and Updating Values, Removing Items, Checking for Keys, What Can Be a Key?, Real-World Use Cases
Lesson Sections
- What is a Dictionary? (concepts: pyDictCreate)
A dictionary in Python is a collection that stores data as key-value pairs. Instead of accessing items by their position (like in a list), you access them by their key. The key is a unique identifier, and the value is whatever data you want to associate with that key. The Real Dictionary Analogy The name "dictionary" comes from actual word dictionaries. In a physical dictionary, you look up a word (the key) to find its definition (the value). You don't say "give me the 4,532nd word." You say "gi
- Creating Dictionaries
Python uses curly braces {} to create dictionaries. Inside the braces, you write key-value pairs separated by colons. Multiple pairs are separated by commas. Let's revisit that user profile problem with a dictionary: Now your data is self-documenting. Anyone reading this code immediately understands what each piece of data represents. The key "name" tells you it's a name. The key "age" tells you it's an age. No memorization required. Syntax Breakdown Understanding the syntax elements helps you r
- Accessing Values (concepts: pyDictMethods)
To retrieve a value from a dictionary, you use square brackets with the key inside. This looks similar to list indexing, but instead of a number, you provide the key name: This is the fundamental operation that makes dictionaries powerful. You're saying "give me the value associated with this key" rather than "give me the value at this position." Handling Missing Keys If you try to access a key that doesn't exist, Python raises a KeyError. This is different from lists, where accessing an invalid
- Adding and Updating Values
Dictionaries are mutable, meaning you can change their contents after creation. Adding a new key-value pair and updating an existing value use the exact same syntax: assign to the key using bracket notation. Notice that Python doesn't distinguish between adding and updating. If the key exists, the value gets replaced. If the key doesn't exist, it gets created. This is convenient but requires you to be careful about typos. The Typo Trap Because adding and updating use the same syntax, a typo in y
- Removing Items
Python provides several ways to remove items from a dictionary. Each method has slightly different behavior, so choosing the right one depends on what you need. The del Statement The .pop() Method Fill in the blanks to remove an entry and check the remaining dictionary size. Clearing a Dictionary Choose the right removal method for the situation below. Think about what happens when the key might not exist.
- Checking for Keys
Checking for Values Dictionary Size Each key-value pair counts as one item. So a dictionary with 3 keys has a length of 3, regardless of how complex the values are.
- What Can Be a Key?
Dictionary keys must be immutable (unchangeable) types. This is because Python uses a special technique called hashing to make key lookups extremely fast. Immutable types can be hashed; mutable types cannot. Keys Must Be Unique Each key in a dictionary must be unique. If you create a dictionary with duplicate keys, only the last value is kept:
- Real-World Use Cases
Dictionaries are everywhere in professional Python code. Here are some common patterns you'll encounter in real data engineering work: Configuration Settings Application configuration is almost always stored in dictionaries. This makes it easy to access settings by name: API Responses When you call a web API, the response typically comes as JSON, which Python represents as nested dictionaries. Here's what weather API data might look like: Counting Occurrences Dictionaries are perfect for countin