Data Structures: Dictionaries and Sets Flashcards
7 cards from real Python practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Data Structures: Dictionaries and Sets flashcards as text
What does `{} == set()` evaluate to?
Answer: False
`{}` creates an empty dictionary, not an empty set; `set()` creates an empty set, so they are not equal.
Given `d = {'x': 10, 'y': 20}`, what is `list(d)`?
Answer: ['x', 'y']
Iterating over a dictionary yields its keys, so `list(d)` produces a list of keys.
What is the output of `s = {3, 1, 2}; print(min(s))`?
Answer: 1
`min()` works on any iterable including sets and returns the smallest element, which is 1.
What does `{1, 2, 3} - {2, 3, 4}` return?
Answer: {1}
The `-` operator on sets returns the difference: elements in the left set that are not in the right set.
Which approach creates a dictionary from two lists `keys` and `values`?
Answer: dict(zip(keys, values))
`zip(keys, values)` pairs elements together and `dict()` converts those pairs into a dictionary.
What does `collections.defaultdict(list)` do when you access a missing key?
Answer: Creates an empty list for that key
`defaultdict` automatically creates a default value (here an empty list) and stores it when a missing key is accessed.
What is the output of `d = {'a': 1}; d.update({'a': 2, 'b': 3}); print(d)`?
Answer: {'a': 2, 'b': 3}
`update()` merges the given dict into `d`, overwriting existing keys with new values and adding new keys.