0
0
Pythonprogramming~10 mins

Inverting a dictionary in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Inverting a dictionary
Start with original dictionary
For each key-value pair
Swap key and value
Add swapped pair to new dictionary
Repeat for all pairs
Return inverted dictionary
End
We take each key and value from the original dictionary, swap them, and put them into a new dictionary until all pairs are processed.
Execution Sample
Python
original = {'a': 1, 'b': 2, 'c': 3}
inverted = {v: k for k, v in original.items()}
print(inverted)
This code creates a new dictionary by swapping keys and values from the original dictionary.
Execution Table
StepCurrent keyCurrent valueActionInverted dictionary state
1'a'1Add 1:'a' to inverted{1: 'a'}
2'b'2Add 2:'b' to inverted{1: 'a', 2: 'b'}
3'c'3Add 3:'c' to inverted{1: 'a', 2: 'b', 3: 'c'}
4No more items-Finish loop{1: 'a', 2: 'b', 3: 'c'}
💡 All key-value pairs processed, loop ends.
Variable Tracker
VariableStartAfter 1After 2After 3Final
original{'a': 1, 'b': 2, 'c': 3}{'a': 1, 'b': 2, 'c': 3}{'a': 1, 'b': 2, 'c': 3}{'a': 1, 'b': 2, 'c': 3}{'a': 1, 'b': 2, 'c': 3}
inverted{}{1: 'a'}{1: 'a', 2: 'b'}{1: 'a', 2: 'b', 3: 'c'}{1: 'a', 2: 'b', 3: 'c'}
Key Moments - 2 Insights
Why do the keys and values swap places in the new dictionary?
Because in the comprehension, we write {v: k} which means the original value becomes the new key, and the original key becomes the new value, as shown in execution_table steps 1-3.
What happens if two original keys have the same value?
The last key processed will overwrite the previous one in the inverted dictionary because dictionary keys must be unique. This is not shown here but is important to know.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the inverted dictionary after step 2?
A{2: 'b'}
B{1: 'a', 2: 'b'}
C{'a': 1, 'b': 2}
D{}
💡 Hint
Check the 'Inverted dictionary state' column at step 2 in the execution_table.
At which step does the loop finish processing all items?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look for the step where 'No more items' is the current key in the execution_table.
If the original dictionary had two keys with value 1, what would happen in the inverted dictionary?
AOnly one key would be stored for key 1, the last one processed
BBoth keys would appear as values for key 1
CAn error would occur
DThe inverted dictionary would be empty
💡 Hint
Recall that dictionary keys must be unique; see key_moments about overwriting.
Concept Snapshot
Inverting a dictionary swaps keys and values.
Use a dictionary comprehension: {v: k for k, v in original.items()}.
Each original value becomes a new key.
Duplicates in values cause overwriting.
Result is a new dictionary with swapped pairs.
Full Transcript
We start with an original dictionary. For each key and value, we swap them and add to a new dictionary. This process repeats until all pairs are processed. The final result is the inverted dictionary where original values are keys and original keys are values. If original values repeat, the last key overwrites previous ones in the inverted dictionary.