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

 

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

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

Let’s jump right into the Python code!

 

Create Dictionary

Here, we will create the dictionary that we will convert to a list of tuples in this tutorial. Therefore, in your preferred Python IDE, run the line of code below to create the dictionary.

my_dict = {"A": 1, "B": 2, "C": 3}

As you can see, the keys are A, B, and C; values are 1, 2, and 3 for the dictionary my_dict.

 

Example 1: Transform Dictionary to List of Tuples Using items() Method

In this first example, we will use the items() method to convert the dictionary to a list of tuples.

list_of_tuples = list(my_dict.items())
print(list_of_tuples)
# [('A', 1), ('B', 2), ('C', 3)]

Now, let’s confirm if the result is indeed a list of tuples via the type() function.

print(type(list_of_tuples))
#<class 'list'>
print(type(list_of_tuples[0]))
#<class 'tuple'>

As shown, we got the right result. Let’s see the next example!

 

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

In this second example, we will use list comprehension via the items() method, which converts the dictionary to a list of tuples, like so:

list_of_tuples = [(key, value) for key, value in my_dict.items()]
print(list_of_tuples)
# [('A', 1), ('B', 2), ('C', 3)]
 
print(type(list_of_tuples))
#<class 'list'>
print(type(list_of_tuples[0]))
#<class 'tuple'>

Well done! Let’s see the next alternative!
 

Example 3: Transform Dictionary to List of Tuples Using For Loop

In this third example, we will loop through the dictionary keys and values and convert those to a list of tuples by appending them to the initialized list list_of_tuples.

list_of_tuples = []
for (i,j) in my_dict.items():
  list_of_tuples.append((i,j))
 
print(list_of_tuples)
# [('A', 1), ('B', 2), ('C', 3)]
 
 
print(type(list_of_tuples))
#<class 'list'>
print(type(list_of_tuples[0]))
#<class 'tuple'>

For loop is a useful method, yet it is not as concise as the previous approaches. Let’s see a more concise method next!

 

Example 4: Transform Dictionary to List of Tuples Using map() Function

In this fourth and final example, we will use Python’s map() function and lambda to iterate through the dictionary and convert its keys and values to a list of tuples.

list_of_tuples = list(map(lambda item: (item[0], item[1]), my_dict.items()))
print(list_of_tuples)
# [('A', 1), ('B', 2), ('C', 3)]
 
 
print(type(list_of_tuples))
#<class 'list'>
print(type(list_of_tuples[0]))
#<class 'tuple'>

So, that’s how to convert a dictionary to a list of tuples in Python. You can select any method depending on your use case. I hope you found this tutorial helpful!

 

Video, Further Resources & Summary

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