Python | Combining values from dictionary of list
Given a dictionary of list values, the task is to combine every key-value pair in every combination.
Input :
{“Name” : [“Paras”, “Chunky”],
“Site” : [“Geeksforgeeks”, “Cyware”, “Google”] }Output:
[{‘Site’: ‘Geeksforgeeks’, ‘Name’: ‘Paras’}, {‘Site’: ‘Cyware’, ‘Name’: ‘Paras’},
{‘Site’: ‘Google’, ‘Name’: ‘Paras’}, {‘Site’: ‘Geeksforgeeks’, ‘Name’: ‘Chunky’},
{‘Site’: ‘Cyware’, ‘Name’: ‘Chunky’}, {‘Site’: ‘Google’, ‘Name’: ‘Chunky’}]
Method #1: Using itertools and sorting
# Python code to combine every key value # pair in every combinations # List initialization Input = { "Bool" : ["True", "False"], "Data" : ["Int", "Float", "Long Long"], } # Importing import itertools as it # Sorting input sorted_Input = sorted(Input) # Using product after sorting Output = [dict(zip(sorted_Input, prod)) for prod in it.product(*(Input[sorted_Input] for sorted_Input in sorted_Input))] # Printing output print(Output) |
[{‘Bool’: ‘True’, ‘Data’: ‘Int’}, {‘Bool’: ‘True’, ‘Data’: ‘Float’}, {‘Bool’: ‘True’, ‘Data’: ‘Long Long’}, {‘Bool’: ‘False’, ‘Data’: ‘Int’}, {‘Bool’: ‘False’, ‘Data’: ‘Float’}, {‘Bool’: ‘False’, ‘Data’: ‘Long Long’}]
Method #2: Using Zip
# Python code to combine every key value # pair in every combinations # Importing import itertools # Input Initialization Input = { "Bool" : ["True", "False"], "Data" : ["Int", "Float", "Long Long"], } # using zip and product without sorting Output = [[{key: value} for (key, value) in zip(Input, values)] for values in itertools.product(*Input.values())] # Printing output print(Output) |
[[{‘Data’: ‘Int’}, {‘Bool’: ‘True’}], [{‘Data’: ‘Int’}, {‘Bool’: ‘False’}], [{‘Data’: ‘Float’}, {‘Bool’: ‘True’}], [{‘Data’: ‘Float’}, {‘Bool’: ‘False’}], [{‘Data’: ‘Long Long’}, {‘Bool’: ‘True’}], [{‘Data’: ‘Long Long’}, {‘Bool’: ‘False’}]]
Recommended Posts:
- Python | Create a dictionary using list with none values
- Python | Element Occurrence in dictionary of list values
- Python | Filter dictionary key based on the values in selective list
- Python dictionary | values()
- Combining multiple columns in Pandas groupby with dictionary
- Python | Check for None values in given dictionary
- Python | Sum values for each key in nested dictionary
- Python | Type conversion in dictionary values
- Python | Filter the negative values from given dictionary
- Map function and Dictionary in Python to sum ASCII values
- Python program to find the highest 3 values in a dictionary
- Python | Find keys with duplicate values in dictionary
- Python | Test if dictionary contains unique keys and values
- Python program to Swap Keys and Values in Dictionary
- Python | Split dictionary keys and values into separate lists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



