- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Combining values from dictionary of list in Python
Let’s say we have a python dictionary which has lists as it values in the key value pairs. We need to create a list which will represent all possible combinations of the keys and values from the given lists.
With sorted and product
The product function from itertools can be used to create a crtesian product of the iterable supplied to it as parameter. We sort the dictionary and use two for loops to create the combination of all possible key value pairs from the lists in the dictionary.
Example
import itertools as it Adict = { "Day": ["Tue", "Wed"], "Time": ["2pm", "9am"], } # Sorting Adict sorted_Adict = sorted(Adict) # Using product after sorting res = [dict(zip(sorted_Adict, prod)) for prod in it.product(*(Adict[sorted_Adict] for sorted_Adict in sorted_Adict))] # Printing output print(res)
Output
Running the above code gives us the following result −
[{'Day': 'Tue', 'Time': '2pm'}, {'Day': 'Tue', 'Time': '9am'}, {'Day': 'Wed', 'Time': '2pm'}, {'Day': 'Wed', 'Time': '9am'}]
With zip
In this approach we use zip the function along with the itertools product function to create the combination of all possible keys and values form the dictionary of lists.
Example
import itertools as it Adict = { "Day": ["Tue", "Wed"], "Time": ["2pm", "9am"], } # Sorting Adict sorted_Adict = sorted(Adict) # Using product after sorting res = [[{key: value} for (key, value) in zip(Adict, values)] for values in it.product(*Adict.values())] # Printing output print(res)
Output
Running the above code gives us the following result −
[[{'Day': 'Tue'}, {'Time': '2pm'}], [{'Day': 'Tue'}, {'Time': '9am'}], [{'Day': 'Wed'}, {'Time': '2pm'}], [{'Day': 'Wed'}, {'Time': '9am'}]]
- Related Articles
- Python – Inverse Dictionary Values List
- How to create Python dictionary from list of keys and values?
- How to get a list of all the values from a Python dictionary?
- Sort Dictionary key and values List in Python
- Combining tuples in list of tuples in Python
- Convert key-values list to flat dictionary in Python
- Python – Create dictionary from the list
- Python – Limit the values to keys in a Dictionary List
- Python Program – Create dictionary from the list
- Python - Create a dictionary using list with none values
- Accessing Values of Dictionary in Python
- Python - Filter dictionary key based on the values in selective list
- Python - Filter the negative values from given dictionary
- How do I sort a list of dictionaries by values of the dictionary in Python?
- Explain how a dataframe structure can be created using list of dictionary values in Python?

Advertisements