Data Structures: Dictionaries and Sets Flashcards
6 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 6 Data Structures: Dictionaries and Sets flashcards as text
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?
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.
What is a key characteristic of the objects returned by the `.keys()`, `.values()`, and `.items()` methods of a dictionary in Python 3?
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.
Which of the following lines of code will result in a `TypeError`?
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.
A programmer needs to find elements that are present in `set_A` but NOT in `set_B`. Which set operation should be used?
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.
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) ```
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.
Which statement correctly describes the fundamental difference between a set and a dictionary?
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.
Written and edited by Nathan Beja, founder of Practice Test Geeks, with AI assistance. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Editorial policy