Create a Dictionary with List Comprehension in Python
Last Updated :
08 Feb, 2025
The task of creating a dictionary with list comprehension in Python involves iterating through a sequence and generating key-value pairs in a concise manner. For example, given two lists, keys = ["name", "age", "city"] and values = ["Alice", 25, "New York"], we can pair corresponding elements using list comprehension and convert them into a dictionary, resulting in {'name': 'Alice', 'age': 25, 'city': 'New York'}.
Using zip()
zip() pairs elements from two lists and is commonly used to create dictionaries efficiently. By wrapping zip() inside a list comprehension and converting it into a dictionary using dict(), we can merge two lists into key-value pairs. It ensures that elements are mapped correctly without extra iterations.
Python
keys = ["name", "age", "city"]
values = ["Alice", 25, "New York"]
d = dict([(k, v) for k, v in zip(keys, values)])
print(d)
Output{'name': 'Alice', 'age': 25, 'city': 'New York'}
Explanation: zip(keys, values) pairs elements into tuples like [('name', 'Alice'), ('age', 25), ('city', 'New York')]. Then, list comprehension generates a list of key-value pairs, which dict() converts into a dictionary, storing it in d.
Using dict()
Dictionary can be created by passing a list of tuples to the dict(). Using list comprehension, we first generate a list of tuples, where each tuple consists of a key-value pair. This method is useful when keys and values are derived through computations, such as squaring numbers or mapping values dynamically.
Python
a = [(x, x**2) for x in range(1, 6)]
d = dict(a)
print(d)
Output{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Explanation: List comprehension generates a list of tuples, where each tuple consists of a number x (from 1 to 5) as the first element and its square (x**2) as the second. Then, this list of tuples is converted into a dictionary using dict().
Using enumerate()
enumerate() assigns an index to each element in an iterable, making it useful for generating dictionaries with numeric keys. By applying list comprehension, we can create a list of (index, value) pairs and convert it into a dictionary. It ensures a structured and ordered mapping of elements.
Python
a = ["apple", "banana", "cherry"]
d = dict([(idx, val) for idx, val in enumerate(a)])
print(d)
Output{0: 'apple', 1: 'banana', 2: 'cherry'}
Explanation: List comprehension iterates over enumerate(a), assigning an index to each element in a, creating tuples (idx, val). These tuples are then passed to dict(), converting them into a dictionary where indexes are keys and list elements are values.
Similar Reads
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
Appending a Dictionary to a List in Python Appending a dictionary allows us to expand a list by including a dictionary as a new element. For example, when building a collection of records or datasets, appending dictionaries to a list can help in managing data efficiently. Let's explore different ways in which we can append a dictionary to a
3 min read
Convert a List to Dictionary Python We are given a list we need to convert the list in dictionary. For example, we are given a list a=[10,20,30] we need to convert the list in dictionary so that the output should be a dictionary like {0: 10, 1: 20, 2: 30}. We can use methods like enumerate, zip to convert a list to dictionary in pytho
2 min read
Python Create Dictionary with Integer The task of creating a dictionary from a list of keys in Python, where each key is assigned a unique integer value, involves transforming the list into a dictionary. Each element in the list becomes a key and the corresponding value is typically its index or a different integer. For example, if we h
3 min read
Create Dictionary from the List-Python The task of creating a dictionary from a list in Python involves mapping each element to a uniquely generated key, enabling structured data storage and quick lookups. For example, given a = ["gfg", "is", "best"] and prefix k = "def_key_", the goal is to generate {'def_key_gfg': 'gfg', 'def_key_is':
3 min read
Convert Dictionary to String List in Python The task of converting a dictionary to a string list in Python involves transforming the key-value pairs of the dictionary into a formatted string and storing those strings in a list. For example, consider a dictionary d = {1: 'Mercedes', 2: 'Audi', 3: 'Porsche', 4: 'Lambo'}. Converting this to a st
3 min read