โ† 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.issubset()` return when called as `{1, 2}.issubset({1, 2, 3})`?

    Answer: True

    `issubset()` returns True if every element of the calling set is also in the argument set.

  2. What is the output of `d = {'a': [1, 2]}; e = d.copy(); e['a'].append(3); print(d['a'])`?

    Answer: [1, 2, 3]

    `dict.copy()` performs a shallow copy, so the list value is shared between `d` and `e`; mutating it through `e` also affects `d`.

  3. Which method removes ALL elements from a set without deleting the set object?

    Answer: .clear()

    `set.clear()` removes all elements in place, leaving an empty set object.

  4. What is the output of `{x**2 for x in range(4)}`?

    Answer: {0, 1, 4, 9}

    Curly braces with a single expression (no colon) and a `for` clause create a set comprehension.

  5. What does `{1, 2, 3}.isdisjoint({4, 5, 6})` return?

    Answer: True

    `isdisjoint()` returns True when two sets share no common elements.

  6. What is the output of `d = dict(a=1, b=2); print('b' in d)`?

    Answer: True

    The `in` operator checks for key membership in a dictionary and returns True because 'b' is a key.

  7. What does `Counter({'a': 3, 'b': 1}) + Counter({'a': 1, 'b': 2})` produce from `collections`?

    Answer: Counter({'a': 4, 'b': 3})

    Adding two Counters sums the counts for each key, resulting in Counter({'a': 4, 'b': 3}).