Chapter Review Questions#
Note
Source: Adapted from the C# edition (dictionaries/reviewdictionaries.rst).
Questions updated for Python dict syntax and idioms.
Is a Python
dictmutable or immutable?What is the syntax to create a dictionary mapping
"a"to 1 and"b"to 2?How do you iterate over all key-value pairs in a dictionary
d?Two ways to look up a key.
What does
d[key]do if the key is not present?What does
d.get(key, default)do instead?When would you prefer the second form?
Comparing dicts and lists.
How is a dictionary similar to a list?
How is it different?
Dictionary key restrictions.
What restriction applies to dictionary keys?
Why does that restriction exist?
What does
d.pop("x", None)do if"x"is not ind?Write one line that counts the number of entries in dictionary
d.Given:
counts = {} for word in ["a", "b", "a", "c", "a", "b"]: counts[word] = counts.get(word, 0) + 1
What is
countsafter the loop?Why is looking up a key in a dictionary O(1) on average, while searching for a value in a list is O(N)?
collections.Counter.What does
collections.Counteradd on top of a plaindict?Give an example use case.
Keys and mutability.
Can a list be used as a dictionary key? Why or why not?
Can a tuple be used as a dictionary key? Why or why not?