Loading lesson...
Key-value pairs power real-world data
Key-value pairs power real-world data
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
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
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
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
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
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 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.
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:
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