Given list values and keys list, convert these values to key value pairs in form of list of dictionaries.
Input : test_list = [“Gfg”, 3, “is”, 8], key_list = [“name”, “id”]
Output : [{‘name’: ‘Gfg’, ‘id’: 3}, {‘name’: ‘is’, ‘id’: 8}]
Explanation : Values mapped by custom key, “name” -> “Gfg”, “id” -> 3.Input : test_list = [“Gfg”, 10], key_list = [“name”, “id”]
Output : [{‘name’: ‘Gfg’, ‘id’: 10}]
Explanation : Conversion of lists to list of records by keys mapping.
Method #1 : Using loop + dictionary comprehension
This is one of the ways in which this task can be performed. In this, we perform mapping values using dictionary comprehension. The iteration is performed using loop.
Python3
# Python3 code to demonstrate working of # Convert List to List of dictionaries # Using dictionary comprehension + loop # initializing lists test_list = ["Gfg", 3, "is", 8, "Best", 10, "for", 18, "Geeks", 33] # printing original list print("The original list : " + str(test_list)) # initializing key list key_list = ["name", "number"] # loop to iterate through elements # using dictionary comprehension # for dictionary construction n = len(test_list) res = [] for idx in range(0, n, 2): res.append({key_list[0]: test_list[idx], key_list[1] : test_list[idx + 1]}) # printing result print("The constructed dictionary list : " + str(res)) |
The original list : [‘Gfg’, 3, ‘is’, 8, ‘Best’, 10, ‘for’, 18, ‘Geeks’, 33]
The constructed dictionary list : [{‘name’: ‘Gfg’, ‘number’: 3}, {‘name’: ‘is’, ‘number’: 8}, {‘name’: ‘Best’, ‘number’: 10}, {‘name’: ‘for’, ‘number’: 18}, {‘name’: ‘Geeks’, ‘number’: 33}]
Method #2 : Using dictionary comprehension + list comprehension
The combination of above functions is used to solve this problem. In ths, we perform a similar task as above method. But difference is that its performed as shorthand.
Python3
# Python3 code to demonstrate working of # Convert List to List of dictionaries # Using zip() + list comprehension # initializing lists test_list = ["Gfg", 3, "is", 8, "Best", 10, "for", 18, "Geeks", 33] # printing original list print("The original list : " + str(test_list)) # initializing key list key_list = ["name", "number"] # using list comprehension to perform as shorthand n = len(test_list) res = [{key_list[0]: test_list[idx], key_list[1]: test_list[idx + 1]} for idx in range(0, n, 2)] # printing result print("The constructed dictionary list : " + str(res)) |
The original list : [‘Gfg’, 3, ‘is’, 8, ‘Best’, 10, ‘for’, 18, ‘Geeks’, 33]
The constructed dictionary list : [{‘name’: ‘Gfg’, ‘number’: 3}, {‘name’: ‘is’, ‘number’: 8}, {‘name’: ‘Best’, ‘number’: 10}, {‘name’: ‘for’, ‘number’: 18}, {‘name’: ‘Geeks’, ‘number’: 33}]
Recommended Posts:
- Python - Convert Dictionaries List to Order Key Nested dictionaries
- Python - Convert List of Dictionaries to List of Lists
- Python - Convert list of dictionaries to Dictionary Value list
- Python - Convert String to List of dictionaries
- Python - Convert Flat dictionaries to Nested dictionary
- Python - Convert String to Nested Dictionaries
- Python - Convert dictionary to K sized dictionaries
- Python - Sort dictionaries list by Key's Value list index
- Ways to sort list of dictionaries by values in Python - Using lambda function
- Ways to sort list of dictionaries by values in Python – Using itemgetter
- Python | Set Difference in list of dictionaries
- Python | Split dictionary of lists to list of dictionaries
- Python | Removing dictionary from list of dictionaries
- Python | Sum list of dictionaries with same key
- Python | Sort given list of dictionaries by date
- Python | Flatten given list of dictionaries
- Python | Merging two list of dictionaries
- Python | Get values of particular key in list of dictionaries
- Python | Segregating key's value in list of dictionaries
- Python | Initialize list with empty dictionaries
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.

