Study Guide: Dictionaries & Tuples
Module 8 Review Materials
Key Terms
Dictionary (dict): A mutable collection of key-value pairs enclosed in curly braces {}.
Key: An immutable identifier used to look up a value in a dictionary. Must be unique.
Value: The data associated with a key. Can be any Python type.
Key-Value Pair: A single entry in a dictionary, written as key: value.
KeyError: The error Python raises when you try to access a dictionary key that does not exist.
Tuple: An ordered, immutable sequence created with parentheses () or commas.
Immutable: Cannot be changed after creation. Tuples and strings are immutable; lists and dictionaries are mutable.
Tuple Unpacking: Assigning each element of a tuple to a separate variable in one statement.
View Object: A dynamic object returned by keys(), values(), or items() that reflects changes to the dictionary.
Nested Dictionary: A dictionary where one or more values are themselves dictionaries.
Lesson 1: Creating and Using Dictionaries
Key Concepts
- Dictionaries map keys to values, providing meaningful labels for data
- Keys must be immutable (strings, numbers, tuples) and unique
- Values can be any Python type, including lists and other dicts
- Access values with d[key] (raises KeyError) or d.get(key, default) (safe)
- Add or update with d[key] = value
- Check membership with key in d
Lesson 2: Dictionary Methods
Key Concepts
- d.keys() returns a view of all keys
- d.values() returns a view of all values
- d.items() returns a view of (key, value) tuples
- d.update(other_dict) merges another dict; existing keys are overwritten
- d.pop(key) removes and returns the value for the given key
- d.clear() removes all entries
- The most common loop pattern: for key, val in d.items()
- Nested dicts: access with chained brackets like d["outer"]["inner"]
Lesson 3: Tuples and Immutability
Key Concepts
- Tuples are ordered, immutable sequences
- Created with parentheses (1, 2, 3) or just commas 1, 2, 3
- Single-element tuple needs a trailing comma: (42,)
- Support indexing and slicing just like lists
- Cannot be modified after creation (no append, remove, or assignment)
- Unpacking: x, y = (3, 7)
- Only two methods: count() and index()
- Can be used as dictionary keys (unlike lists)
Lesson 4: Choosing Data Structures
Key Concepts
- List: ordered, mutable — for collections that change (shopping carts, to-do lists)
- Tuple: ordered, immutable — for fixed data where position has meaning (coordinates, RGB colors)
- Dictionary: key-value mapping — for labeled data and fast lookups (phone books, settings)
- Combine structures: lists of dicts, dicts with list values, etc.
Common Mistakes to Avoid
- Forgetting the trailing comma in single-element tuples: (42) is an int, (42,) is a tuple
- Using a list as a dictionary key — only immutable types (strings, numbers, tuples) can be keys
- Not handling KeyError — use get() or check with in before accessing
- Confusing in behavior — for dicts, in checks keys, not values
- Trying to modify a tuple — tuples are immutable; create a new tuple instead
- Mismatched unpacking — the number of variables must match the tuple length
Review Questions
- What is the difference between d["key"] and d.get("key")?
- Name three types that can be used as dictionary keys.
- How do you loop through both keys and values of a dictionary?
- What is tuple unpacking? Give an example.
- Why would you choose a tuple over a list?
- How do you create a single-element tuple?
- What method would you use to merge two dictionaries?
- Can a dictionary value be a list? Can a dictionary key be a list?
- What data structure would you use for an address book? Why?
- How do you swap two variables using tuple unpacking?