Python - Convert List to List of dictionaries
Last Updated :
23 Jan, 2025
We are given a lists with key and value pair we need to convert the lists to List of dictionaries. For example we are given two list a=["name", "age", "city"] and b=[["Geeks", 25, "New York"], ["Geeks", 30, "Los Angeles"], ["Geeks", 22, "Chicago"]] we need to convert these keys and values list into list of dictionaries so that the output should be [{'name': 'Geeks', 'age': 25, 'city': 'New York'}, {'name': 'Geeks', 'age': 30, 'city': 'Los Angeles'}, {'name': 'Geeks', 'age': 22, 'city': 'Chicago'}] . For this we can use multiple methods like zip, dictionary comprehension ,map.
Using zip()
zip() function pairs keys from a with values from each sublist in b and dict() converts these pairs into dictionaries, which are collected into a list using list comprehension.
Python
a = ["name", "age", "city"]
b = [["Alice", 25, "New York"], ["Bob", 30, "Los Angeles"], ["Charlie", 22, "Chicago"]]
# Create a list of dictionaries by zipping keys with each sublist of values
ans = [dict(zip(a, values)) for values in b]
print(ans)
Output[{'name': 'Alice', 'age': 25, 'city': 'New York'}, {'name': 'Bob', 'age': 30, 'city': 'Los Angeles'}, {'name': 'Charlie', 'age': 22, 'city': 'Chicago'}]
Explanation:
- zip(a, values) pairs keys from a with corresponding values from each sublist in b, creating key-value pairs.
- List comprehension converts each paired sublist into a dictionary using dict() and collects all dictionaries into a list.
Using a Dictionary Comprehension
Dictionary comprehension {key: value for key, value in zip(a, values)} creates a dictionary by pairing keys from a with values from a sublist in b using zip(). A list comprehension wraps this to generate a list of dictionaries for all sublists in b.
Python
a = [["Alice", 25, "New York"], ["Bob", 30, "Los Angeles"], ["Charlie", 22, "Chicago"]] # List of values
b = ["name", "age", "city"] # List of keys
# Create dictionaries by pairing keys from 'b' with values from each sublist in 'a'
res = [{b[i]: value[i] for i in range(len(b))} for value in a]
print(res)
Output[{'name': 'Alice', 'age': 25, 'city': 'New York'}, {'name': 'Bob', 'age': 30, 'city': 'Los Angeles'}, {'name': 'Charlie', 'age': 22, 'city': 'Chicago'}]
Explanation:
- List comprehension iterates over each sublist in a and creates a dictionary by pairing keys from b with corresponding values from the sublist using their index positions.
- Each generated dictionary is added to a list, resulting in a list of dictionaries where each dictionary represents a set of key-value pairs from a sublist in a.
Using map()
map() function applies a function to each sublist in a, where the function zips the keys from b with the values in the sublist and converts them into dictionaries. The result is then collected into a list using list().
Python
a = ["name", "age", "city"] # Keys for dictionaries
b = [["Alice", 25, "New York"], ["Bob", 30, "Los Angeles"], ["Charlie", 22, "Chicago"]] # Values
# Use map with a lambda to zip keys and values, then convert to a list of dictionaries
res = list(map(lambda x: dict(zip(a, x)), b))
print(res)
Output[{'name': 'Alice', 'age': 25, 'city': 'New York'}, {'name': 'Bob', 'age': 30, 'city': 'Los Angeles'}, {'name': 'Charlie', 'age': 22, 'city': 'Chicago'}]
Explanation:
- map() function applies a lambda that zips each sublist in b (values) with the keys from a, creating key-value pairs.
- map() function produces an iterator, which is then converted to a list of dictionaries, where each dictionary represents a key-value mapping from the zipped values.
Using a Loop
A for
loop iterates through each sublist in b
and a dictionary comprehension creates a dictionary by pairing keys from a
with corresponding values from the sublist. Each dictionary is appended to the result list res
Python
a = ["name", "age", "city"] # List of keys
b = [["Alice", 25, "New York"], ["Bob", 30, "Los Angeles"], ["Charlie", 22, "Chicago"]] # List of values
# Iterate through each sublist in 'b' and create a dictionary by pairing keys from 'a' with values from the sublist
res = []
for values in b:
res.append({a[i]: values[i] for i in range(len(a))}) # Create a dictionary and append it to the result list
print(res)
Output[{'name': ['Alice', 25, 'New York'], 'age': ['Bob', 30, 'Los Angeles'], 'city': ['Charlie', 22, 'Chicago']}, {'name': ['Alice', 25, 'New York'], 'age': ['Bob', 30, 'Los Angeles'], 'city': ['Charlie', ...
Explanation:
- for loop iterates over each sublist in b and a dictionary comprehension is used to pair each key from a with the corresponding value in the sublist.
- Each generated dictionary is appended to the list res, which accumulates all the dictionaries corresponding to the sublists in b.
Similar Reads
Convert List of Lists to Dictionary - Python We are given list of lists we need to convert it to python . For example we are given a list of lists a = [["a", 1], ["b", 2], ["c", 3]] we need to convert the list in dictionary so that the output becomes {'a': 1, 'b': 2, 'c': 3}. Using Dictionary ComprehensionUsing dictionary comprehension, we ite
3 min read
Python - Convert List of Dictionaries to List of Lists We are given list of dictionaries we need to convert it to list of lists. For example we are given a list of dictionaries a = [{'name': 'Geeks', 'age': 25}, {'name': 'Geeks', 'age': 30}] we need to convert it in list of list so that the output becomes[['Geeks',25],['Geeks;'30]].Using List Comprehens
3 min read
Convert List of Dictionary to Tuple list Python Given a list of dictionaries, write a Python code to convert the list of dictionaries into a list of tuples.Examples: Input: [{'a':[1, 2, 3], 'b':[4, 5, 6]}, {'c':[7, 8, 9], 'd':[10, 11, 12]}] Output: [('b', 4, 5, 6), ('a', 1, 2, 3), ('d', 10, 11, 12), ('c', 7, 8, 9)] Below are various methods to co
5 min read
Python - Convert String to List of dictionaries Given List of dictionaries in String format, Convert into actual List of Dictionaries. Input : test_str = ["[{'Gfg' : 3, 'Best' : 8}, {'Gfg' : 4, 'Best' : 8}]"] Output : [[{'Gfg': 3, 'Best': 8}, {'Gfg': 4, 'Best': 8}]] Explanation : String converted to list of dictionaries. Input : test_str = ["[{'G
4 min read
Convert List of Dictionaries to Dictionary of Lists - Python We are given a list of dictionaries we need to convert it to dictionaries of lists. For example, we are given a list of dictionaries li = [{'manoj': 'java', 'bobby': 'python'}, {'manoj': 'php', 'bobby': 'java'}, {'manoj': 'cloud', 'bobby': 'big-data'}] we need to convert this to dictionary of list s
3 min read
Python - Convert Key-Value list Dictionary to List of Lists We are given a key value list dictionary we need to convert it list of lists. For example we are given a dictionary a = {'name': 'Geeks', 'age': 8, 'city': 'Noida'} we need to convert this into list of lists so the output should be [['name', 'Geeks'], ['age', 25], ['city', 'Geeks']]. Using List Comp
2 min read
Convert Nested Dictionary to List in Python In this article, weâll explore several methods to Convert Nested Dictionaries to a List in Python. List comprehension is the fastest and most concise way to convert a nested dictionary into a list.Pythona = { "a": {"x": 1, "y": 2}, "b": {"x": 3, "y": 4}, } # Convert nested dictionary to a list of li
3 min read
Convert List Of Dictionary into String - Python In Python, lists can contain multiple dictionaries, each holding key-value pairs. Sometimes, we need to convert a list of dictionaries into a single string. For example, given a list of dictionaries [{âaâ: 1, âbâ: 2}, {âcâ: 3, âdâ: 4}], we may want to convert it into a string that combines the conte
3 min read
Python - Convert list of dictionaries to Dictionary Value list We are given a list of dictionaries we need to convert it to dictionary. For example, given a list of dictionaries: d = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6}], the output should be: {'a': [1, 3, 5], 'b': [2, 4, 6]}.Using Dictionary ComprehensionUsing dictionary comprehension, we can
3 min read
Convert a Dictionary to a List in Python In Python, dictionaries and lists are important data structures. Dictionaries hold pairs of keys and values, while lists are groups of elements arranged in a specific order. Sometimes, you might want to change a dictionary into a list, and Python offers various ways to do this. How to Convert a Dict
3 min read