Convert Two Lists into a Dictionary – Python
We are given two lists, we need to convert both of the list into dictionary. For example we are given two lists a = [“name”, “age”, “city”], b = [“Geeks”, 30,”Delhi”], we need to convert these two list into a form of dictionary so that the output should be like {‘name’: ‘Geeks’, ‘age’: 30, ‘city’: ‘Delhi’}. We can do this using methods like zip, dictionary comprehension , itertools.starmap.
Using zip
Use zip to pair elements from two lists, where the first list provides the keys and second provides the values after that we convert the zipped object into a dictionary using dict() which creates key-value pairs.
a = ["name", "age", "city"]
b = ["Alice", 30, "New York"]
# Create dictionary using zip
res = dict(zip(a, b))
print(res)
Output
{'name': 'Alice', 'age': 30, 'city': 'New York'}
Explanation:
- zip(a, b) pairs each element from list a with the corresponding element from list b, creating tuples of key-value pairs.
- dict() function is used to convert the zipped pairs into a dictionary where elements from a become the keys and elements from b become values
Using a Loop
Iterate through both lists simultaneously using zip and for each pair, add the first element as the key and second as the value to the dictionary.
a = ["name", "age", "city"]
b = ["Alice", 30, "New York"]
res = {}
# Iterate through the lists using zip
for key, value in zip(a, b):
res[key] = value
print(res)
Output
{'name': 'Alice', 'age': 30, 'city': 'New York'}
Explanation:
- An empty dictionary res is created, and zip(a, b) is used to iterate through both lists yielding pairs of keys and values.
- During each iteration, key from list a is added to the dictionary with its corresponding value from list b
Using Dictionary Comprehension
Use dictionary comprehension to iterate over the pairs generated by zip(a, b), creating key-value pairs where elements from list a are the keys and elements from list b are the values. This creates the dictionary in a single concise expression.
a = ["name", "age", "city"]
b = ["Alice", 30, "New York"]
# Create dictionary using dictionary comprehension
res = {key: value for key, value in zip(a, b)}
print(res)
Output
{'name': 'Alice', 'age': 30, 'city': 'New York'}
Explanation:
- Dictionary comprehension iterates over pairs generated by zip(a, b), where each pair consists of a key from list a and a value from list b.
- For each pair the key-value pair is directly added to dictionary res in one concise expression.
Using itertools.starmap
Use itertools.starmap to apply a lambda function that takes two arguments (key and value) to each pair generated by zip(a, b). This creates key-value pairs and passes them directly into dict() to form dictionary.
from itertools import starmap
a = ["name", "age", "city"]
b = ["Alice", 30, "New York"]
# Create dictionary using starmap
res = dict(starmap(lambda k, v: (k, v), zip(a, b)))
print(res)
Output
{'name': 'Alice', 'age': 30, 'city': 'New York'}
Explanation:
- starmap applies a lambda function to each pair from zip(a, b), where each pair consists of a key and a value.
- lambda function returns the key-value pair (k, v) and dict() converts the results into a dictionary