← 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 is the output of `d = {}; d[('a', 'b')] = 1; print(len(d))`?

    Answer: 1

    Tuples are hashable and can be used as dictionary keys; the dictionary has one key-value pair so its length is 1.

  2. Which expression produces a frozenset?

    Answer: frozenset([1, 2, 3])

    `frozenset()` accepts an iterable and returns an immutable, hashable set.

  3. What does `{k: v for k, v in [('a', 1), ('b', 2)]}` produce?

    Answer: {'a': 1, 'b': 2}

    This dict comprehension unpacks each two-element tuple into key-value pairs, building a dictionary.

  4. What is the result of `{1, 2} | {3, 4}`?

    Answer: {1, 2, 3, 4}

    The `|` operator on sets returns the union — all elements from both sets with no duplicates.

  5. Which statement about Python dictionaries is TRUE as of Python 3.7+?

    Answer: Insertion order is preserved

    Since Python 3.7, dictionaries maintain insertion order as part of the language specification.

  6. What does `d.items()` return?

    Answer: A view of (key, value) tuples

    `dict.items()` returns a dynamic view object of (key, value) pairs that reflects changes to the dictionary.

  7. What happens when you try to add a list to a set: `s = {1, 2}; s.add([3, 4])`?

    Answer: TypeError: unhashable type

    Lists are mutable and unhashable, so they cannot be added to a set; Python raises TypeError.