Skip to content

Python: Convert a Dictionary to a List of Tuples (4 Easy Ways)

Python Convert Dictionary to List of Tuples Cover Image

In this tutorial, you’ll learn how to use Python to convert a dictionary into a list of tuples. You’ll learn how to do this using the list() function, Python list comprehensions, and the zip() function.

Knowing how to work with dictionaries and how to safely convert them to other Python data structures is an incredibly useful skill for any beginner or advanced Pythonista.

The Quick Answer: Use the list() function

Quick Answer - Python Convert Dictionary to List of Tuples

Convert a Python Dictionary to a List of Tuples Using the List Function

One of the most straightforward and Pythonic ways to convert a Python dictionary into a list of tuples is to the use the built-in list() function. This function takes an object and generates a list with its items.

One of the built-in methods for dictionaries is the .items() methods, which returns a tuple of tuples of the key value pairs found inside the dictionary. We can use this method and pass it into the list() function, in order to generate a list of tuples that contain the key value pairs from our dictionary.

# Convert a Dictionary into a List of Tuples Using list()

sample_dict = {'john': 30, 'nik': 32, 'datagy': 40}
list_of_tuples = list(sample_dict.items())

print(list_of_tuples)
# Returns: [('john', 30), ('nik', 32), ('datagy', 40)]

We can see here that we passed our dictionaries items into the list() function. This returned a list of tuples, with corresponding key-value pairs.

In the next section, you’ll learn how to use a Python list comprehension to convert a dict into a list of tuples.

Want to learn how to pretty print a JSON file using Python? Learn three different methods to accomplish this using this in-depth tutorial here.

Convert a Python Dictionary to a List of Tuples Using a List Comprehension

Python list comprehensions are elegant ways in which to generate lists based on another iterable object. In order to convert a dictionary in Python to a list of tuples, we will loop over the keys and values and generate tuples.

Let’s take a look at what this code looks like:

# Convert a Dictionary into a List of Tuples Using a List Comprehension

sample_dict = {'john': 30, 'nik': 32, 'datagy': 40}
list_of_tuples = [(key, value) for key, value in sample_dict.items()]

print(list_of_tuples)
# Returns: [('john', 30), ('nik', 32), ('datagy', 40)]

Let’s break down our list comprehension a little bit:

  1. Our iterable is represented by the sample_dict.items(), which returns a tuple of tuples of our key value pairs
  2. We iterate over these by creating a new tuple of the key-value pairs

In the next section, you’ll learn how to use the Python zip() function to convert a dict to a list of tuples.

Want to learn more about Python list comprehensions? Check out this in-depth tutorial that covers off everything you need to know, with hands-on examples. More of a visual learner, check out my YouTube tutorial here.

Convert a Python Dictionary to a List of Tuples Using the Zip Function

The Python zip() function allows you to iterate in sequence over multiple iterative objects. We can create two iterative objects for a dictionary’s keys and values by using the .keys() and .items() methods. Both of these returns iterable objects, containing either the keys or the values of our dictionary.

Let’s take a look at how we can do this in Python:

# Convert a Dictionary into a List of Tuples Using the Zip Function

sample_dict = {'john': 30, 'nik': 32, 'datagy': 40}
keys = sample_dict.keys()
values = sample_dict.values()
list_of_tuples = list(zip(keys, values))

print(list_of_tuples)
# Returns: [('john', 30), ('nik', 32), ('datagy', 40)]

Let’s take a look at what we’ve done here:

  1. We loaded our sample dictionary
  2. We created two new objects, one containing the keys and one containing the values
  3. We used the zip() function to combine these two objects together, in sequence
  4. We converted it to a list of tuples

In the next section, you’ll learn how to use the collections library to convert a dict to a list of tuples.

Want to learn how to use the Python zip() function to iterate over two lists? This tutorial teaches you exactly what the zip() function does and shows you some creative ways to use the function.

Convert a Python Dictionary to a List of Tuples Using a For Loop

A very intuitive way to accomplish converting a Python dict to a list of tuples is to use a Python for loop.

Python for-loops allow us to easily iterate over a sequence and perform a provided action as a result.

Let’s take a look at how we can accomplish this, using a Python for loop:

# Convert a Dictionary into a List of Tuples Using a for loop

sample_dict = {'john': 30, 'nik': 32, 'datagy': 40}

list_of_tuples = list()
for key in sample_dict:
    list_of_tuples.append((key, sample_dict.get('key')))

print(list_of_tuples)
# Returns: [('john', 30), ('nik', 32), ('datagy', 40)]

Let’s explore this code a little bit to better understand what is going on:

  1. We generated an empty list, to which we will append our tuples
  2. We loop over each key in the sample_dict object
  3. We append a tuple containing the key and the value that the key returns using the .get() method

Need to check if a key exists in a Python dictionary? Check out this tutorial, which teaches you five different ways of seeing if a key exists in a Python dictionary, including how to return a default value.

Conclusion

In this post, you learned how to use Python to convert a dictionary to a list of tuples. Being able to convert and move between different Python data structures is an incredibly useful skill to learn. You learned how to do this using the list() function, Python list comprehensions, and the zip() function

To learn more about the Python collections library, check out the official documentation here.

Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

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