Python Data Structures: Dictionaries and Sets Questions and Answers — Questions and Answers
Question 1: A developer is processing a large log file and needs to store unique IP addresses to count the visitors. The order of the IP addresses is not important. Which data structure is the most memory-efficient and performant for this specific task?
- A list, because it is simple to append to.
- A dictionary, using IP addresses as keys.
- A tuple, because it is immutable.
- A set, because it only stores unique elements. (Correct answer)
Correct answer: A set, because it only stores unique elements.
A set is the ideal choice because it enforces uniqueness automatically and provides highly efficient membership testing. While a dictionary could work, it would waste memory storing `None` or a dummy value for each key. Lists and tuples do not enforce uniqueness, requiring extra processing to filter out duplicates.
Question 2: What is a key characteristic of the objects returned by the `.keys()`, `.values()`, and `.items()` methods of a dictionary in Python 3?
- They are static lists containing the dictionary's data at the time of the call.
- They are immutable tuples, preventing any modification.
- They are dynamic views that reflect subsequent changes to the dictionary. (Correct answer)
- They are iterators that are exhausted after a single pass.
Correct answer: They are dynamic views that reflect subsequent changes to the dictionary.
In Python 3, these methods return dictionary view objects. A key feature of view objects is that they are dynamic and provide a live view into the dictionary's entries. If the dictionary is modified, the view object reflects these changes immediately.
Question 3: Which of the following lines of code will result in a `TypeError`?
- my_dict = {(1, 2): 'a'}
- my_set = {1, 'hello', 3.14}
- my_dict = {['user', 'id']: 123} (Correct answer)
- my_set = {frozenset([1, 2]), frozenset([3, 4])}
Correct answer: my_dict = {['user', 'id']: 123}
Dictionary keys must be of an immutable (and hashable) type. Lists are mutable, so they cannot be used as dictionary keys, which will raise a `TypeError`. Tuples, integers, strings, and frozensets are all immutable and can be used as keys or stored in sets.
Question 4: A programmer needs to find elements that are present in `set_A` but NOT in `set_B`. Which set operation should be used?
- set_A.union(set_B)
- set_A.intersection(set_B)
- set_A.difference(set_B) (Correct answer)
- set_A.symmetric_difference(set_B)
Correct answer: set_A.difference(set_B)
The `.difference()` method (or the `-` operator) returns a new set containing elements that are in the first set but not in the second set. `union()` combines all elements, `intersection()` finds common elements, and `symmetric_difference()` finds elements that are in either set, but not both.
Question 5: Consider the following code snippet. What will be the final content of `inventory`? ```python inventory = {'apples': 10, 'oranges': 5} updates = {'oranges': 15, 'grapes': 20} inventory.update(updates) ```
- {'apples': 10, 'oranges': 5, 'grapes': 20}
- {'oranges': 15, 'grapes': 20}
- {'apples': 10, 'oranges': 15, 'grapes': 20} (Correct answer)
- The code will raise a KeyError because 'grapes' is a new key.
Correct answer: {'apples': 10, 'oranges': 15, 'grapes': 20}
The `.update()` method merges the keys and values from one dictionary into another. If a key already exists in the original dictionary, its value is updated with the value from the new dictionary. If a key does not exist, the new key-value pair is added.
Question 6: Which statement correctly describes the fundamental difference between a set and a dictionary?
- Sets are mutable, while dictionaries are immutable.
- A set is an ordered collection, while a dictionary is unordered.
- A set stores unique, individual elements, while a dictionary stores key-value pairs. (Correct answer)
- Sets can only store strings, while dictionaries can store any data type.
Correct answer: A set stores unique, individual elements, while a dictionary stores key-value pairs.
The primary distinction is their structure. A set is a collection of unique, hashable elements. A dictionary, on the other hand, is a collection of unique keys mapped to values, forming key-value pairs. Both are mutable, and as of Python 3.7, dictionaries maintain insertion order.
A developer is processing a large log file and needs to store unique IP addresses to count the visitors.
The order of the IP addresses is not important.
Which data structure is the most memory-efficient and performant for this specific task?