
Python Tutorial for Beginners 5: Dictionaries - Working with Key-Value Pairs
Corey Schafer
Overview
This video introduces Python dictionaries, a fundamental data structure for storing information as key-value pairs. It explains how dictionaries are created, accessed, and modified, covering essential operations like retrieving values, adding new entries, updating existing ones, and deleting data. The tutorial also demonstrates how to handle missing keys gracefully using the `get()` method and how to iterate through dictionary contents. Dictionaries are compared to real-world dictionaries and other programming concepts like hashmaps, highlighting their utility for organizing and retrieving data efficiently.
Save this permanently with flashcards, quizzes, and AI chat
Chapters
- Dictionaries store data as unique key-value pairs, similar to a physical dictionary where a word (key) maps to its definition (value).
- They are also known as hashmaps or associative arrays in other programming languages.
- Keys must be unique and immutable (like strings or numbers), while values can be of any data type, including lists or other dictionaries.
- Values are accessed using their corresponding keys within square brackets (e.g., `student['name']`).
- Attempting to access a non-existent key directly raises a `KeyError`.
- The `get()` method provides a safer way to access values, returning `None` or a specified default value if the key is not found, preventing errors.
- New key-value pairs can be added by assigning a value to a new key using square bracket notation (e.g., `student['phone'] = '555-5555'`).
- Assigning a value to an existing key updates its value.
- The `update()` method allows for adding multiple new pairs or updating multiple existing pairs simultaneously by passing another dictionary.
- The `del` keyword can be used to remove a specific key-value pair (e.g., `del student['age']`).
- The `pop()` method removes a key-value pair and returns the removed value, allowing it to be stored or used.
- Both methods effectively remove the specified entry from the dictionary.
- The `len()` function returns the number of key-value pairs in a dictionary.
- `.keys()` returns a view object of all keys, `.values()` returns a view object of all values, and `.items()` returns a view object of (key, value) tuples.
- To loop through both keys and values, iterate over `dictionary.items()`, unpacking each pair into two variables (e.g., `for key, value in student.items():`).
- A simple `for key in dictionary:` loop iterates only through the keys.
Key takeaways
- Dictionaries are Python's primary tool for storing unordered collections of data using unique, immutable keys.
- Keys act as labels to quickly retrieve associated values, making data lookup very efficient.
- Use the `get()` method with an optional default value to safely access dictionary elements and avoid `KeyError` exceptions.
- Dictionaries are mutable, allowing for dynamic addition, updating, and deletion of key-value pairs.
- The `update()` method is efficient for applying multiple changes to a dictionary at once.
- The `del` keyword and `pop()` method offer different ways to remove data, with `pop()` also returning the removed value.
- Iterating through `dictionary.items()` is the standard way to access both keys and values simultaneously within a loop.
Key terms
Test your understanding
- What is the fundamental difference between a dictionary key and a dictionary value?
- Why is it generally safer to use the `get()` method instead of direct key access when retrieving dictionary values?
- How can you add a new piece of information (e.g., an email address) to an existing student dictionary?
- What are the two primary ways to remove data from a dictionary, and what is the key difference in their behavior?
- How would you write a loop to print out both the name of each course and the list of students enrolled in that course, assuming you had a dictionary structured for this?