Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python – Inverse Dictionary Values List
Inverting a dictionary means swapping keys and values. When the original dictionary has lists as values, we create a new dictionary where each item in those lists becomes a key, and the original keys become the values.
Using defaultdict
The defaultdict from collections module automatically creates empty lists for new keys ?
from collections import defaultdict
my_dict = {13: [12, 23], 22: [31], 34: [21], 44: [52, 31]}
print("The original dictionary is:")
print(my_dict)
inverted_dict = defaultdict(list)
for key, values in my_dict.items():
for value in values:
inverted_dict[value].append(key)
print("The inverted dictionary is:")
print(dict(inverted_dict))
The original dictionary is:
{13: [12, 23], 22: [31], 34: [21], 44: [52, 31]}
The inverted dictionary is:
{12: [13], 23: [13], 31: [22, 44], 21: [34], 52: [44]}
Using Regular Dictionary
Without defaultdict, we need to check if a key exists before appending ?
my_dict = {13: [12, 23], 22: [31], 34: [21], 44: [52, 31]}
print("The original dictionary is:")
print(my_dict)
inverted_dict = {}
for key, values in my_dict.items():
for value in values:
if value in inverted_dict:
inverted_dict[value].append(key)
else:
inverted_dict[value] = [key]
print("The inverted dictionary is:")
print(inverted_dict)
The original dictionary is:
{13: [12, 23], 22: [31], 34: [21], 44: [52, 31]}
The inverted dictionary is:
{12: [13], 23: [13], 31: [22, 44], 21: [34], 52: [44]}
Using Dictionary Comprehension
A more compact approach using dictionary comprehension and setdefault() ?
my_dict = {13: [12, 23], 22: [31], 34: [21], 44: [52, 31]}
print("The original dictionary is:")
print(my_dict)
inverted_dict = {}
for key, values in my_dict.items():
for value in values:
inverted_dict.setdefault(value, []).append(key)
print("The inverted dictionary is:")
print(inverted_dict)
The original dictionary is:
{13: [12, 23], 22: [31], 34: [21], 44: [52, 31]}
The inverted dictionary is:
{12: [13], 23: [13], 31: [22, 44], 21: [34], 52: [44]}
How It Works
We iterate through each key-value pair in the original dictionary
For each list of values, we iterate through individual items
Each item becomes a key in the new dictionary
The original key is appended to the list of values for that new key
Multiple original keys can map to the same value, creating lists in the result
Comparison
| Method | Pros | Cons |
|---|---|---|
defaultdict |
Clean, no key checking needed | Requires import |
| Regular dictionary | No imports needed | Manual key existence checking |
setdefault() |
Compact, built-in method | Less readable for beginners |
Conclusion
Dictionary inversion with list values creates a mapping where original values become keys and original keys become grouped values. Use defaultdict for cleaner code or setdefault() for a more compact solution.
