Python | Dictionary creation using list contents
Sometimes we need to handle the data coming in the list format and convert list into dictionary format. This particular problem is quite common while we deal with Machine Learning to give further inputs in changed formats. Let’s discuss certain ways in which this inter conversion happens.
Method #1 : Using dictionary comprehension + zip()
In this method, we use dictionary comprehension to perform the iteration and logic part, the binding of all the lists into one dictionary and with associated keys is done by zip function.
# Python3 code to demonstrate # Dictionary creation using list contents # using Dictionary comprehension + zip() # initializing list keys_list = ["key1", "key2"] nested_name = ["Manjeet", "Nikhil"] nested_age = [22, 21] # printing original lists print("The original key list : " + str(keys_list)) print("The original nested name list : " + str(nested_name)) print("The original nested age list : " + str(nested_age)) # using Dictionary comprehension + zip() # Dictionary creation using list contents res = {key: {'name': name, 'age': age} for key, name, age in zip(keys_list, nested_name, nested_age)} # print result print("The dictionary after construction : " + str(res)) |
The original key list : [‘key1’, ‘key2’]
The original nested name list : [‘Manjeet’, ‘Nikhil’]
The original nested age list : [22, 21]
The dictionary after construction : {‘key1’: {‘age’: 22, ‘name’: ‘Manjeet’}, ‘key2’: {‘age’: 21, ‘name’: ‘Nikhil’}}
Method #2 : Using dictionary comprehension + enumerate()
The similar task can be performed using enumerate function that was performed by the zip function. The dictionary comprehension performs the task similar as above.
# Python3 code to demonstrate # Dictionary creation using list contents # using dictionary comprehension + enumerate() # initializing list keys_list = ["key1", "key2"] nested_name = ["Manjeet", "Nikhil"] nested_age = [22, 21] # printing original lists print("The original key list : " + str(keys_list)) print("The original nested name list : " + str(nested_name)) print("The original nested age list : " + str(nested_age)) # using dictionary comprehension + enumerate() # Dictionary creation using list contents res = {val : {"name": nested_name[key], "age": nested_age[key]} for key, val in enumerate(keys_list)} # print result print("The dictionary after construction : " + str(res)) |
The original key list : [‘key1’, ‘key2’]
The original nested name list : [‘Manjeet’, ‘Nikhil’]
The original nested age list : [22, 21]
The dictionary after construction : {‘key1’: {‘age’: 22, ‘name’: ‘Manjeet’}, ‘key2’: {‘age’: 21, ‘name’: ‘Nikhil’}}
Recommended Posts:
- Python | Modifying tuple contents with list
- Python | List value merge in dictionary
- Python | Clearing list as dictionary value
- Python | Get dictionary keys as a list
- Python | Updating value list in dictionary
- Python | Convert a list to dictionary
- Python | Create a dictionary using list with none values
- Python | Removing dictionary from list of dictionaries
- Python | Grouping list values into dictionary
- Python | Convert dictionary to list of tuples
- Python | Converting list string to dictionary
- Python | Find dictionary matching value in list
- Print anagrams together in Python using List and Dictionary
- Python | Sort the list alphabetically in a dictionary
- Python | Unique dictionary filter in list
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.



