• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Dictionary / Convert a Dictionary to List of Tuples in Python

Convert a Dictionary to List of Tuples in Python

Author: Aditya Raj
Last Updated: September 10, 2021

We know that a dictionary in python contains key-value pairs. In this article, we will convert a python dictionary to a list of tuples where each tuple contains a key-value pair. 

Table of Contents
  • Convert a dictionary to a list of tuples using a for loop
  • Convert a dictionary to a list of tuples using the items() method
  • Convert a dictionary to a list of tuples using the zip() function
  • Conclusion

Convert a dictionary to a list of tuples using a for loop

We can convert a python dictionary to a list of tuples using a for loop by accessing the keys and values of the dictionary one by one. First, we will create tuples using keys and values and then we will append them to a list as follows.

myDict = {1: 2, 3: 4, 5: 6, 7: 8}
print("The Dictionary is:",myDict)
myList = []
for key in myDict:
    value = myDict[key]
    myTuple = (key, value)
    myList.append(myTuple)
print("The list of tuples is:",myList)

Output:

The Dictionary is: {1: 2, 3: 4, 5: 6, 7: 8}
The list of tuples is: [(1, 2), (3, 4), (5, 6), (7, 8)]

Convert a dictionary to a list of tuples using the items() method

The items() method, when invoked on a dictionary, returns a dict_items object. The dict_items object contains the key-value pairs of the dictionary. We can convert the dict_items to a list using the list() method as follows.

myDict = {1: 2, 3: 4, 5: 6, 7: 8}
print("The Dictionary is:", myDict)
items = myDict.items()
myList = list(items)
print("The list of tuples is:", myList)

Output:

The Dictionary is: {1: 2, 3: 4, 5: 6, 7: 8}
The list of tuples is: [(1, 2), (3, 4), (5, 6), (7, 8)]

Instead of using the list() function, we can use list comprehension to convert the dict_items to a list as follows.

myDict = {1: 2, 3: 4, 5: 6, 7: 8}
print("The Dictionary is:", myDict)
items = myDict.items()
myList = [item for item in items]
print("The list of tuples is:", myList)

Output:

The Dictionary is: {1: 2, 3: 4, 5: 6, 7: 8}
The list of tuples is: [(1, 2), (3, 4), (5, 6), (7, 8)]

Convert a dictionary to a list of tuples using the zip() function

We can use the zip() function to create a list of tuples containing the key-value pairs. The zip() function takes iterable objects such as lists as input and merges them into a single iterable object. While merging, the zip() function creates tuples of elements at the same index from each iterable.

For example, If we pass  two lists [1, 2, 3] and [4, 5, 6] to the zip() function, it merges the lists into [(1, 4), (2, 5), (3, 6)]. i.e. the elements at the same index from each iterable are zipped together.

To convert a dictionary to a list of tuples using the zip() function, we will pass the list of keys and the list of values as input to the zip() function.

We can obtain the list of keys using the keys() method. The keys() method, when invoked on a dictionary, returns a dict_keys object containing the keys of the dictionary. We can convert the dict_keys object to a list using the list() function.

Similarly, We can obtain the list of values using the values() method. The values() method, when invoked on a dictionary, returns a dict_values object containing the values of the dictionary. We can convert the dict_values object to a list using the list() function.

We can convert a dictionary to a list of tuples using the keys() and values() method, and zip() function as follows.

myDict = {1: 2, 3: 4, 5: 6, 7: 8}
print("The Dictionary is:", myDict)
keys = list(myDict.keys())
values = list(myDict.values())
myList = list(zip(keys, values))
print("The list of tuples is:", myList)

Output:

The Dictionary is: {1: 2, 3: 4, 5: 6, 7: 8}
The list of tuples is: [(1, 2), (3, 4), (5, 6), (7, 8)]

Conclusion

In this article, we have discussed different ways to convert a dictionary to a List of Tuples in Python. We have used for loop, list comprehension and zip() functions along with dictionary methods like items(), keys(), and values() to convert the dictionary to a list of tuples.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Dictionary Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us