Conditional Statements
The len() function returns the number of keys in a dictionary, which is also the number of key-value pairs.
From Position to Meaning
A dictionary is a data structure that stores key-value pairs. Instead of finding a value by a numeric position, you find it by using a meaningful key. This makes a dictionary useful when the identifier for a piece of information is more useful than its position.
A contacts dictionary can connect each person's name to that person's email address. The name is the key, and the email address is the value. To find a person's email address, you use the person's name rather than a numeric position.
Creating Key-Value Pairs
Dictionaries are created with curly braces. Inside the braces, each key is followed by a colon and its corresponding value. Multiple key-value pairs are separated by commas. The general structure is {key1: value1, key2: value2}. Keys must be unique and immutable.
| Dictionary part | Purpose |
|---|---|
| Curly braces | Enclose the dictionary |
| Key | Provides the meaningful identifier |
| Colon | Separates a key from its value |
| Value | Stores the information associated with the key |
| Comma | Separates one pair from the next |
The parts of dictionary syntax
Looking Up and Changing Entries
Working with the contacts Dictionary
Use the contacts dictionary to understand the main dictionary operations.
- 1Access
Use
dictionary_name[key] to retrieve the value associated with a key. For the contacts dictionary, placing a person's name inside square brackets retrieves that person's email address. - 2Add
Use
dictionary_name[new_key] =new_valueto add a new key-value pair. - 3Modify
Use
dictionary_name[existing_key] =new_valueto update the value associated with an existing key. - 4Delete
Use del
dictionary_name[key] to permanently remove both the selected key and its associated value.
A dictionary is mutable: after it is created, entries can be added, modified, or deleted.
The len() function returns the number of keys in a dictionary, which is also the number of key-value pairs.
Processing Every Pair
When you need to process every key-value pair, use the .items() method with a for loop. The pattern for key, value in dictionary.items(): provides both parts of each pair during each iteration. The pair is unpacked into the variables named key and value.
- Use for key, value in
dictionary.items(): when both keys and values are needed. - Use for key in dictionary: when only keys are needed.
- Use for value in
dictionary.values(): when only values are needed.
Mistakes with Dictionaries
Common mistake Treating a dictionary as if its values were found by numeric position
What it looks likeTrying to retrieve a dictionary value by a numeric position instead of using its key
Why it failsA dictionary is designed for lookup by meaningful keys rather than numeric position.
Do this insteadUse the required key inside square brackets.
Common mistake Using a key that does not exist without checking
What it looks likeAccessing or deleting a key before testing whether it is present
Why it failsThe key may not exist in the dictionary.
Do this insteadUse the in operator to check key membership first.
Common mistake Assuming an assignment always adds a new entry
What it looks likeAssigning a value using a key that already exists
Why it failsAssigning to an existing key updates its value rather than creating another entry with the same key.
Do this insteadDecide whether the operation is intended to add a new key or modify an existing value.
Common mistake Deleting only the value conceptually
What it looks likeUsing del on a dictionary entry without recognizing what is removed
Why it failsThe del operation removes both the selected key and its associated value.
Do this insteadUse del dictionary_name[key] when the entire key-value pair should be removed.
Practice and Review
A contacts dictionary stores names as keys and email addresses as values. Describe which dictionary operation you would use to retrieve one person's email address, add a new contact, update an existing email address, remove a contact, check whether a contact exists, and process every contact.
Hints
- Retrieval uses a key inside square brackets.
- Adding and modifying both use assignment with a key.
- Deletion uses del.
- Membership checking uses in.
- Processing both parts of every pair uses .items().
- 1A dictionary organizes information as key-value pairs. Curly braces create the structure, keys identify values, and keys must be unique and immutable. Square-bracket lookup retrieves a value, assignment adds or updates an entry, and del removes an entry. The in operator checks whether a key exists. The .items() method supports iteration through both keys and values, while dictionary iteration can also process only keys or only values.
Key Takeaways
- Dictionaries store key-value pairs and retrieve values by meaningful keys rather than numeric position.
- Dictionary syntax uses curly braces, colons between keys and values, and commas between pairs.
- Dictionaries are mutable: entries can be accessed, added, modified, and deleted.
- The in operator checks key existence, and
len()counts key-value pairs. - The .items(), dictionary, and .values() iteration patterns retrieve pairs, keys, or values.