Convert List of Tuples to Dictionary in Python (3 Examples)

 

Hello! This tutorial will show you 3 ways to convert a list of tuples into a dictionary in the Python programming language.

First, though, here is an overview of this tutorial:

Let’s dive into the code!

 

Create List of Tuples

Tuples are used to store multiple items in one variable, and they are immutable, meaning they can’t be changed. To create the list of tuples that we will use in this tutorial, run the line of code below in your Python IDE:

my_tuples = [("Name", "John"),("Age", 25),("Occupation", "Analyst")]

Now we can convert the created my_tuples to a dictionary, which is a mutable object.

 

Example 1: Transform List of Tuples to Dictionary via dict() Function

In this first example, we will use the Python dict() function to convert the list of tuples into a dictionary:

my_dict = dict(my_tuples)
print(my_dict)
 
# {'Name': 'John', 'Age': 25, 'Occupation': 'Analyst'}

 

Example 2: Transform List of Tuples to Dictionary via Dictionary Comprehension

In this second example, we are going to use dictionary comprehension to convert the list of tuples into a dictionary:

my_dict = {i[0]: i[1] for i in my_tuples}
print(my_dict)
 
# {'Name': 'John', 'Age': 25, 'Occupation': 'Analyst'}

 

Example 3: Transform List of Tuples to Dictionary via for Loop

In this third and final example, we will use a for loop and the setdefault() method to convert the list of tuples into a dictionary:

my_dict = {}
for i,j in my_tuples:
  my_dict.setdefault(i,[]).append(j)
print(my_dict)
 
# {'Name': ['John'], 'Age': [25], 'Occupation': ['Analyst']}

So there you have it! We have demonstrated 3 simple ways to convert a list of tuples into a dictionary in Python.

I hope you found this helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to convert a list of tuples into 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 tuples into 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 tuples into 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