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 is the output of `d = {}; d[('a', 'b')] = 1; print(len(d))`?
Answer: 1
Tuples are hashable and can be used as dictionary keys; the dictionary has one key-value pair so its length is 1.
Which expression produces a frozenset?
Answer: frozenset([1, 2, 3])
`frozenset()` accepts an iterable and returns an immutable, hashable set.
What does `{k: v for k, v in [('a', 1), ('b', 2)]}` produce?
Answer: {'a': 1, 'b': 2}
This dict comprehension unpacks each two-element tuple into key-value pairs, building a dictionary.
What is the result of `{1, 2} | {3, 4}`?
Answer: {1, 2, 3, 4}
The `|` operator on sets returns the union — all elements from both sets with no duplicates.
Which statement about Python dictionaries is TRUE as of Python 3.7+?
Answer: Insertion order is preserved
Since Python 3.7, dictionaries maintain insertion order as part of the language specification.
What does `d.items()` return?
Answer: A view of (key, value) tuples
`dict.items()` returns a dynamic view object of (key, value) pairs that reflects changes to the dictionary.
What happens when you try to add a list to a set: `s = {1, 2}; s.add([3, 4])`?
Answer: TypeError: unhashable type
Lists are mutable and unhashable, so they cannot be added to a set; Python raises TypeError.