Convert a list of Tuples into Dictionary - Python
Last Updated :
07 Apr, 2025
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 pair from the tuples matches a valid dictionary structure, the expected output is {'a': 1, 'b': 2, 'c': 3}. Let's explore different methods to achieve this.
Using dict()
dict() function converts an iterable of key-value pairs, such as a list of tuples, into a dictionary. It assigns the first element of each tuple as the key and the second as the corresponding value.
Python
a = [("a", 1), ("b", 2), ("c", 3)]
res = dict(a)
print(res)
Output{'a': 1, 'b': 2, 'c': 3}
Explanation: dict(a) constructor iterates through the list of tuples a , extracting the first element of each tuple as a key and the second as its corresponding value, forming a dictionary.
Using dictionary comprehension
Dictionary comprehension allows creating a dictionary in a single line by iterating over an iterable and specifying key-value pairs. It uses the syntax {key: value for item in iterable} to construct the dictionary efficiently.
Python
a = [("a", 1), ("b", 2), ("c", 3)]
res = {key: value for key, value in a}
print(res)
Output{'a': 1, 'b': 2, 'c': 3}
Explanation: {key: value for key, value in a} iterates through each tuple, assigning the first element as the key and the second as the value, efficiently constructing a dictionary.
Using for loop
Using a for
loop to create a dictionary involves iterating over an iterable and adding each element as a key-value pair. This can be done by manually assigning values to a dictionary within the loop.
Python
a = [("a", 1), ("b", 2), ("c", 3)]
res = {}
# Populate the dictionary
for key, value in a:
res[key] = value
print(res)
Output{'a': 1, 'b': 2, 'c': 3}
Explanation: for loop iterates through each tuple, assigning the first element as the key and the second as the value. Each key-value pair is added to res, constructing the dictionary.
Using map() with dict()
map() function applies a given function to each element in an iterable, and when used with dict(), it transforms the result into key-value pairs. This allows for efficient mapping and conversion into a dictionary.
Python
a = [("a", 1), ("b", 2), ("c", 3)]
res = dict(map(lambda x: (x[0], x[1]), a))
print(res)
Output{'a': 1, 'b': 2, 'c': 3}
Explanation: map() function applies a lambda function to each tuple, extracting the first element as the key and the second as the value. The dict() constructor then converts the mapped key-value pairs into a dictionary.
Similar Reads
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 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': '
3 min read
Convert List Of Tuples To Json Python Working with data often involves converting between different formats, and JSON is a popular choice for data interchange due to its simplicity and readability. In Python, converting a list of tuples to JSON can be achieved through various approaches. In this article, we'll explore four different met
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
Create a List of Tuples in Python The task of creating a list of tuples in Python involves combining or transforming multiple data elements into a sequence of tuples within a list. Tuples are immutable, making them useful when storing fixed pairs or groups of values, while lists offer flexibility for dynamic collections. For example
3 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
Convert Tuple to List in Python In Python, tuples and lists are commonly used data structures, but they have different properties:Tuples are immutable: their elements cannot be changed after creation.Lists are mutable: they support adding, removing, or changing elements.Sometimes, you may need to convert a tuple to a list for furt
2 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 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 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