Convert List of Key-Value Pairs to Dictionary in Python (3 Examples)

 

Hi! This tutorial will show you 3 simple ways to transform a list of key-value pairs into a dictionary in the Python programming language.

First, though, here is a quick overview of this tutorial:

Let’s jump into the Python code!

 

Create Sample List of Key-Value Pairs

Here, we will create the sample list of key-value pairs that we will transform into a Python dictionary. Therefore, in your Python IDE of choice, run the line of code below.

pairs = [("a", 1), ("b", 2), ("c", 3)]

The above is a list of tuples, containing the keys and values that we will turn into a dictionary.
 

Example 1: Turn List of Key-Value Pairs into Dictionary Using dict() Function

In this first example, we will use the dict() function to turn the list of key-value pairs into a dictionary.

new_dict = dict(pairs)
print(new_dict)
 
# {'a': 1, 'b': 2, 'c': 3}

The program above converts the list of key-value pairs into a Python dictionary. However, we can also confirm that it is indeed a dictionary by running:

print(type(new_dict))
 
# <class 'dict'>

The class of the Python object is thus returned.
 

Example 2: Turn List of Key-Value Pairs into Dictionary Using Dictionary Comprehension

In this second example, we will turn the list of key-value pairs into a Python dictionary using dictionary comprehension.

new_dict = {key: value for key, value in pairs}
print(new_dict)
# {'a': 1, 'b': 2, 'c': 3}
 
 
print(type(new_dict))
# <class 'dict'>

Looks like our attempt is indeed successful.
 

Example 3: Turn List of Key-Value Pairs into Dictionary Using a For Loop

In this third and final example, we will loop through the list of key-value pairs and then parse the loop to the setdefault() method, which will convert it into a dictionary.

new_dict = {}
for i, j in pairs:
  new_dict.setdefault(i, j)
 
print(new_dict)
#1
#2
#3
# {'a': 1, 'b': 2, 'c': 3}
 
 
print(type(new_dict))
# <class 'dict'>

The setdefault() method returns the value of the item, along with its specified key. Thus, the pairs tuple turned into a dictionary called new_dict.

With that, we have demonstrated 3 simple ways to transform a list of key-value pairs into a dictionary in the Python programming language. Depending on your use case, you can use any of the above-demonstrated methods. I hope you found this tutorial helpful, and I will see you in the next one!
 

Video, Further Resources & Summary

Do you need more explanations on how to convert a list of key-value pairs to a dictionary in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to convert a list of key-value pairs to a dictionary in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to convert a list of key-value pairs to a dictionary in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top