In this article, How to convert list of tuples to python dictionary, the list is represented by this bracket square brackets([]), the list can have duplicate elements, whereas the dictionary is represented by curly parenthesis {key: value}, the dictionary has unique keys, but the value can be duplicated.
Ways to Convert a list of tuples to the dictionary
- Using setdefault()
- using dict()
- dictionary comprehension
Let us understand each of them with a code example
1. setdefault() to Convert a list of tuples to dictionary
In our first example, we will use the Python dictionary method setdefault(). This function returns the value of a key(if the key exists in the dictionary), else it inserts the key with a value to the dictionary.
Syntax of setdefault()
dict.setdefault(key,defualtvalue)
Parameters of setdefault() function
The setdefault() takes two arguments
- Key: It represents the key we are looking for in the dictionary.
- Defaultvalue: It is an optional parameter if the key with default value interested in the dictionary if the key does not exist in the dictionary. If value not provided then it will be none.
Code example
# Program to convert a list of tuples to dictionary in python
#defining an empty dictionary
lang_dictionary = {}
lang_tuples= [("C#", 1), ("C", 2), ("C++", 3), ("Go", 20), ("Python", 25), ("Rust", 30)]
#converting a list of the tuple to the dictionary
for lang,value in lang_tuples:
#list of tuples to the dictionary
lang_dictionary.setdefault(lang, []).append(value)
print('list of tuples to dictionary=\n', lang_dictionary)
Output:
list of tuples to dictionary=
{'C#': [1], 'C': [2], 'C++': [3], 'Go': [20], 'Python': [25], 'Rust': [30]}
2. list of tuples to dictionary using dict()
In this example, we will use a dict() constructor for creating a new dictionary.Dict() does not return any value. Here in the below code, we are passing a list of tuples as arguments to dict() and converting it to the dictionary.
Output: Showing the converted dictionary from list of tuples.
Code example
# Program to convert a list of tuples to dictionary in python
list_of_tuples= [("C#", 1), ("C", 2), ("C++", 3),
("Go", 20), ("Python", 25), ("Rust", 30)]
#converting a list of the tuple to the dictionary
lang_dictionary = dict(list_of_tuples)
print('list of tuples to dictionary=\n', lang_dictionary)
Output:
list of tuples to dictionary =
{'C#': 1, 'C': 2, 'C++': 3, 'Go': 20, 'Python': 25, 'Rust': 30}
3.Convert list of tuples to Dictionary
In this code example, we are using dictionary comprehension to convert a list of tuples to the dictionary.
Syntax for dictionary comprehension
{key:value for(key,value) in iterable}
We are converting a list of tuples [(key, value)……(key_n, value_n)] into the dictionary of {key: value) by using dictionary comprehension
Code example
# Program to convert a list of tuples to dictionary in python
list_of_tuples= [("C#", 1), ("C", 2), ("C++", 3),
("Go", 20), ("Python", 25), ("Rust", 30)]
##converting a list of the tuple to the dictionary
lang_dictionary ={list_of_tuples[i][0]: list_of_tuples[i][1] for i in range(0, len(list_of_tuples), 1)}
print('list of tuples to dictionary=\n', lang_dictionary)
Output
list of tuples to dictionary =
{'C#': 1, 'C': 2, 'C++': 3, 'Go': 20, 'Python': 25, 'Rust': 30}
Conclusion
We hope you understand all the three ways to convert a list of tuples to a python dictionary.
Happy learning !