Convert Two Lists into a Dictionary - Python
Last Updated :
18 Apr, 2025
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. Let's implement these methods practically.
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.
Python
a = ["name", "age", "city"]
b = ["Alice", 30, "New York"]
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 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.
Python
a = ["name", "age", "city"]
b = ["Alice", 30, "New York"]
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 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.
Python
a = ["name", "age", "city"]
b = ["Alice", 30, "New York"]
res = {}
for k, v in zip(a, b):
res[k] = v
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"
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.
Python
from itertools import starmap
a = ["name", "age", "city"]
b = ["Alice", 30, "New York"]
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.
Related Articles:
Similar Reads
Convert a list of Tuples into Dictionary - Python Converting a list of tuples into a dictionary involves transforming each tuple, where the first element serves as the key and the second as the corresponding value. For example, given a list of tuples a = [("a", 1), ("b", 2), ("c", 3)], we need to convert it into a dictionary. Since each key-value p
3 min read
Python | Convert list of tuple into dictionary Given a list containing all the element and second list of tuple depicting the relation between indices, the task is to output a dictionary showing the relation of every element from the first list to every other element in the list. These type of problems are often encountered in Coding competition
8 min read
Convert Lists to Nested Dictionary - Python The task of converting lists to a nested dictionary in Python involves mapping elements from multiple lists into key-value pairs, where each key is associated with a nested dictionary. For example, given the lists a = ["gfg", "is", "best"], b = ["ratings", "price", "score"], and c = [5, 6, 7], the g
3 min read
Python - Converting list string to dictionary Converting a list string to a dictionary in Python involves mapping elements from the list to key-value pairs. A common approach is pairing consecutive elements, where one element becomes the key and the next becomes the value. This results in a dictionary where each pair is represented as a key-val
3 min read
How to Create a Dictionary in Python The task of creating a dictionary in Python involves storing key-value pairs in a structured and efficient manner, enabling quick lookups and modifications. A dictionary is an unordered, mutable data structure where each key must be unique and immutable, while values can be of any data type. For exa
3 min read
Ways to create a dictionary of Lists - Python A dictionary of lists is a type of dictionary where each value is a list. These dictionaries are commonly used when we need to associate multiple values with a single key.Initialize a Dictionary of ListsThis method involves manually defining a dictionary where each key is explicitly assigned a list
3 min read
How to convert a MultiDict to nested dictionary using Python A MultiDict is a dictionary-like object that holds multiple values for the same key, making it a useful data structure for processing forms and query strings. It is a subclass of the Python built-in dictionary and behaves similarly. In some use cases, we may need to convert a MultiDict to a nested d
3 min read
How To Convert Generator Object To Dictionary In Python We are given a generator object we need to convert that object to dictionary. For example, a = (1, 2, 3), b = ('a', 'b', 'c') we need to convert this to dictionary so that the output should be {1: 'a', 2: 'b', 3: 'c'}.Using a Generator ExpressionA generator expression can be used to generate key-val
3 min read
Ways to convert string to dictionary To convert a String into a dictionary, the stored string must be in such a way that a key: value pair can be generated from it. For example, a string like "{'a': 1, 'b': 2, 'c': 3}" or "a:1, b:10" can be converted into a dictionary This article explores various methods to perform this conversion eff
2 min read
Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
2 min read