How to convert list of tuples to python dictionary

In this article, How to convert list of tuples to python dictionary or list of key-value pair to the 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.

How to convert list of tuples to python dictionary


  • Using setdefault()
  • using dict()
  • dictionary comprehension

1. setdefault() to Convert a list of tuples to Python 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 into 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 is interested in the dictionary if the key does not exist in the dictionary. If value not provided then it will be none.

Python Program to convert list of tuples to python dictionary

# 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. Convert list of tuples to Python 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 the dict() function and converting it to the dictionary.Output: Showing the converted dictionary from a 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 Python Dictionary


In this code example, we are using dictionary comprehension to convert a list of tuples to a dictionary. We are converting a list of tuples [(key, value)……(key_n, value_n)] into the dictionary of {key: value) by using dictionary comprehension

The syntax for dictionary comprehension

{key:value for(key,value) in iterable}

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

In this post, we have learned How to convert list of tuples to python dictionary We hope you understand all the three ways to convert a list of tuples to a python dictionary. Happy learning!