โ† All Python Flashcard Decks

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
  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.