Bird
Raised Fist0
Pythonprogramming~20 mins

Inverting a dictionary in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
Dictionary Inversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of simple dictionary inversion
What is the output of this Python code that inverts a dictionary?
Python
original = {'a': 1, 'b': 2, 'c': 3}
inverted = {v: k for k, v in original.items()}
print(inverted)
ATypeError
B{'a': 1, 'b': 2, 'c': 3}
C{'1': 'a', '2': 'b', '3': 'c'}
D{1: 'a', 2: 'b', 3: 'c'}
Attempts:
2 left
๐Ÿ’ก Hint
Think about swapping keys and values in the dictionary comprehension.
โ“ Predict Output
intermediate
2:00remaining
Inverting dictionary with duplicate values
What is the output of this code that tries to invert a dictionary with duplicate values?
Python
original = {'a': 1, 'b': 2, 'c': 1}
inverted = {v: k for k, v in original.items()}
print(inverted)
A{1: 'c', 2: 'b'}
BKeyError
C{'a': 1, 'b': 2, 'c': 1}
D{1: 'a', 2: 'b', 1: 'c'}
Attempts:
2 left
๐Ÿ’ก Hint
When keys repeat in a dictionary, the last one stays.
๐Ÿ”ง Debug
advanced
2:00remaining
Identify the error in dictionary inversion code
What error does this code raise when trying to invert a dictionary?
Python
original = {'a': [1, 2], 'b': [3, 4]}
inverted = {v: k for k, v in original.items()}
print(inverted)
ASyntaxError
BNo error, prints inverted dictionary
CTypeError
DKeyError
Attempts:
2 left
๐Ÿ’ก Hint
Dictionary keys must be immutable types.
๐Ÿง  Conceptual
advanced
1:30remaining
Number of items after inverting dictionary with unique values
If a dictionary has 5 unique keys and 5 unique values, how many items will the inverted dictionary have?
A10
B5
C0
DDepends on the values
Attempts:
2 left
๐Ÿ’ก Hint
Inversion swaps keys and values one-to-one if all are unique.
โ“ Predict Output
expert
2:30remaining
Output of inverting dictionary with tuple values
What is the output of this code that inverts a dictionary with tuple values?
Python
original = {'x': (1, 2), 'y': (3, 4)}
inverted = {v: k for k, v in original.items()}
print(inverted)
A{(1, 2): 'x', (3, 4): 'y'}
B{'x': (1, 2), 'y': (3, 4)}
CTypeError
D{'(1, 2)': 'x', '(3, 4)': 'y'}
Attempts:
2 left
๐Ÿ’ก Hint
Tuples are immutable and can be dictionary keys.

Practice

(1/5)
1.

What does inverting a dictionary mean in Python?

easy
A. Sorting the dictionary by keys
B. Swapping keys and values so values become keys and keys become values
C. Removing duplicate keys from the dictionary
D. Changing all values to uppercase strings

Solution

  1. Step 1: Understand dictionary structure

    A dictionary has keys and values paired together.
  2. Step 2: Define inverting

    Inverting means swapping each key with its value, so keys become values and values become keys.
  3. Final Answer:

    Swapping keys and values so values become keys and keys become values -> Option B
  4. Quick Check:

    Inverting = swapping keys and values [OK]
Hint: Invert means swap keys and values in a dictionary [OK]
Common Mistakes:
  • Thinking inverting sorts the dictionary
  • Confusing inverting with removing duplicates
  • Assuming values become uppercase strings
2.

Which of the following is the correct syntax to invert a dictionary d using dictionary comprehension?

easy
A. {k: v for k, v in d}
B. {k: v for v, k in d.items()}
C. {d[v]: d[k] for k, v in d.items()}
D. {v: k for k, v in d.items()}

Solution

  1. Step 1: Recall dictionary comprehension syntax

    It uses {new_key: new_value for key, value in dict.items()}.
  2. Step 2: Swap keys and values correctly

    To invert, new_key = value and new_value = key, so use {v: k for k, v in d.items()}.
  3. Final Answer:

    {v: k for k, v in d.items()} -> Option D
  4. Quick Check:

    Correct syntax = {v: k for k, v in d.items()} [OK]
Hint: Use {v: k for k, v in d.items()} to invert dictionary [OK]
Common Mistakes:
  • Swapping variables incorrectly in comprehension
  • Using d[v] or d[k] inside comprehension wrongly
  • Forgetting to call .items() on dictionary
3.

What is the output of this code?

original = {'a': 1, 'b': 2, 'c': 3}
inverted = {v: k for k, v in original.items()}
print(inverted)

medium
A. {1: 'a', 2: 'b', 3: 'c'}
B. {'a': 1, 'b': 2, 'c': 3}
C. {'1': 'a', '2': 'b', '3': 'c'}
D. Error: unhashable type

Solution

  1. Step 1: Understand original dictionary

    Keys are 'a', 'b', 'c' and values are 1, 2, 3.
  2. Step 2: Invert dictionary using comprehension

    Swapping keys and values gives keys 1, 2, 3 and values 'a', 'b', 'c'.
  3. Final Answer:

    {1: 'a', 2: 'b', 3: 'c'} -> Option A
  4. Quick Check:

    Inverted dict = {1: 'a', 2: 'b', 3: 'c'} [OK]
Hint: Invert swaps keys and values exactly as pairs [OK]
Common Mistakes:
  • Expecting original dictionary output
  • Confusing string and integer keys
  • Thinking inversion causes error here
4.

What is wrong with this code to invert a dictionary?

d = {'x': 10, 'y': 10}
inverted = {v: k for k, v in d.items()}
print(inverted)

medium
A. It will keep only one key for duplicate values
B. It will invert correctly with no issues
C. It will raise a TypeError
D. It will raise a KeyError

Solution

  1. Step 1: Identify duplicate values in dictionary

    Both 'x' and 'y' have value 10, which is duplicated.
  2. Step 2: Understand dictionary key uniqueness

    When inverting, keys must be unique, so only one key-value pair with key 10 remains.
  3. Final Answer:

    It will keep only one key for duplicate values -> Option A
  4. Quick Check:

    Duplicate values cause lost keys in inversion [OK]
Hint: Duplicate values become keys, only last key kept [OK]
Common Mistakes:
  • Expecting all keys preserved after inversion
  • Thinking it raises an error for duplicates
  • Ignoring key uniqueness in dictionaries
5.

Given a dictionary with possible duplicate values, how can you invert it so each value maps to a list of keys that had that value?

original = {'a': 1, 'b': 2, 'c': 1}

Which code correctly inverts it to {1: ['a', 'c'], 2: ['b']}?

hard
A. inverted = {v: [k] for k, v in original.items()}
B. inverted = {v: k for k, v in original.items()}
C. inverted = {} for k, v in original.items(): inverted.setdefault(v, []).append(k)
D. inverted = {v: k for v, k in original.items()}

Solution

  1. Step 1: Understand problem with duplicates

    Simple inversion loses keys when values repeat, so we need lists to hold multiple keys.
  2. Step 2: Use setdefault and append to collect keys

    Loop through items, for each value use setdefault to create list if missing, then append key.
  3. Step 3: Check code correctness

    inverted = {} for k, v in original.items(): inverted.setdefault(v, []).append(k) uses this approach correctly, building lists of keys per value.
  4. Final Answer:

    inverted = {} for k, v in original.items(): inverted.setdefault(v, []).append(k) -> Option C
  5. Quick Check:

    Use setdefault + append to group keys by value [OK]
Hint: Use setdefault with append to group keys by value [OK]
Common Mistakes:
  • Using simple comprehension losing duplicate keys
  • Swapping variables incorrectly in comprehension
  • Expecting one-to-one inversion with duplicates