Python – Convert a list of Tuples into Dictionary
We are given a list of tuples we need to convert that list in form of dictionary.
For example li=[(“a”, 1), (“b”, 2), (“c”, 3)] is a list containing multiple tuples and we need to convert this in dictionary so that the output should be like {‘a’: 1, ‘b’: 2, ‘c’: 3} . To do this we can use methods like dict, dictionary comprehension , map and various other methods.
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.
# List of tuples where each tuple represents a key-value pair
a = [("a", 1), ("b", 2), ("c", 3)]
# Convert the list of tuples into a dictionary
res = dict(a)
print(res)
Output
{'a': 1, 'b': 2, 'c': 3}
Explanation:
- List a contains tuples, where each tuple consists of a key-value pair, such as (“a”, 1).
- dict() function converts the list of tuples into a dictionary mapping each key to its corresponding value.
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.
# List of tuples where each tuple represents a key-value pair
a = [("a", 1), ("b", 2), ("c", 3)]
# Create a dictionary using dictionary comprehension, iterating over the pairs
res = {key: value for key, value in a}
print(res)
Output
{'a': 1, 'b': 2, 'c': 3}
Explanation:
- List
a
contains tuples where each tuple has a key-value pair, such as (“a”, 1). - Dictionary comprehension is used to iterate through the list
a
and create a dictionary by assigning the first element of each tuple as the key and the second as the value.
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.
a = [("a", 1), ("b", 2), ("c", 3)]
# Initialize an empty dictionary to store the result
res = {}
# Iterate through each tuple in the list, unpacking into 'key' and 'value'
for key, value in a:
# Add the key-value pair to the dictionary
res[key] = value
print(res)
Output
{'a': 1, 'b': 2, 'c': 3}
Explanation:
- Code initializes an empty dictionary
res
and iterates over the lista
, unpacking each tuple intokey
andvalue
. - In each iteration, the
key
is used to assign thevalue
to theres
dictionary, building the dictionary step by step.
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.
a = [("a", 1), ("b", 2), ("c", 3)]
# Use map to apply a lambda function to each tuple, creating key-value pairs
res = dict(map(lambda x: (x[0], x[1]), a))
print(res)
Output
{'a': 1, 'b': 2, 'c': 3}
Explanation:
map()
function applies the lambda function to each tuple in the lista
, which extracts the first and second elements as key-value pairs.dict()
function then converts the output ofmap()
into a dictionary, resulting in key-value pairs like{'a': 1, 'b': 2, 'c': 3}
.