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 – All combinations of a Dictionary List
When you need to create all possible combinations of dictionaries from keys and values, Python's itertools.product function combined with list comprehension provides an elegant solution. This technique generates the Cartesian product of values and maps them to corresponding keys.
Basic Example
Here's how to create all combinations where keys from one list are paired with all possible value combinations from another list ?
from itertools import product
keys = ["python", "is", "fun"]
values = [24, 15]
print("The keys list is:")
print(keys)
print("The values list is:")
print(values)
# Generate all combinations using product
temp = product(values, repeat=len(keys))
# Create dictionaries for each combination
result = [{key: value for (key, value) in zip(keys, element)} for element in temp]
print("The result is:")
for i, dictionary in enumerate(result):
print(f"Combination {i+1}: {dictionary}")
The keys list is:
['python', 'is', 'fun']
The values list is:
[24, 15]
The result is:
Combination 1: {'python': 24, 'is': 24, 'fun': 24}
Combination 2: {'python': 24, 'is': 24, 'fun': 15}
Combination 3: {'python': 24, 'is': 15, 'fun': 24}
Combination 4: {'python': 24, 'is': 15, 'fun': 15}
Combination 5: {'python': 15, 'is': 24, 'fun': 24}
Combination 6: {'python': 15, 'is': 24, 'fun': 15}
Combination 7: {'python': 15, 'is': 15, 'fun': 24}
Combination 8: {'python': 15, 'is': 15, 'fun': 15}
How It Works
The solution works in three key steps:
product(values, repeat=len(keys))generates all possible combinations of values with repetition allowedzip(keys, element)pairs each key with its corresponding value from each combinationList comprehension creates a dictionary for each combination using dictionary comprehension
Different Value Lists Example
You can also create combinations using different value sets ?
from itertools import product
keys = ["name", "age", "city"]
names = ["Alice", "Bob"]
ages = [25, 30]
cities = ["NYC", "LA"]
# Generate combinations from different lists
combinations = product(names, ages, cities)
result = [{key: value for (key, value) in zip(keys, combo)} for combo in combinations]
print("All combinations:")
for i, person in enumerate(result):
print(f"Person {i+1}: {person}")
All combinations:
Person 1: {'name': 'Alice', 'age': 25, 'city': 'NYC'}
Person 2: {'name': 'Alice', 'age': 25, 'city': 'LA'}
Person 3: {'name': 'Alice', 'age': 30, 'city': 'NYC'}
Person 4: {'name': 'Alice', 'age': 30, 'city': 'LA'}
Person 5: {'name': 'Bob', 'age': 25, 'city': 'NYC'}
Person 6: {'name': 'Bob', 'age': 25, 'city': 'LA'}
Person 7: {'name': 'Bob', 'age': 30, 'city': 'NYC'}
Person 8: {'name': 'Bob', 'age': 30, 'city': 'LA'}
Conclusion
Using itertools.product with list comprehension efficiently generates all possible dictionary combinations. This approach is particularly useful for testing scenarios, configuration generation, or data modeling where you need to explore all possible parameter combinations.
