Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Convert List of Dictionary to Tuple list Python
Next article icon

Python | Dictionary to list of tuple conversion

Last Updated : 22 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Inter conversion between the datatypes is a problem that has many use cases and is usual subproblem in the bigger problem to solve. The conversion of tuple to dictionary has been discussed before. This article discusses a converse case in which one converts the dictionary to list of tuples as the way to represent the problem. Let's discuss certain ways in which this problem can be solved. 

Method #1 : Using list comprehension + tuple + items() This problem can be solved by using list comprehension for the construction of list and the tuples are constructed by manually inserting the keys in the tuples and items function is used to fetch the items key and values of dictionary in form of tuples. 

Python3
# Python3 code to demonstrate  # Dictionary to list of tuple conversion  # using list comprehension + tuple + items()   # initializing Dictionary  test_dict = {"Nikhil" : (22, "JIIT"), "Akshat" : (21, "JIIT")}   # printing original dictionary  print("The original dictionary : " + str(test_dict))   # using list comprehension + tuple + items()  # Dictionary to list of tuple conversion  res = [(key, i, j) for key, (i, j) in test_dict.items()]   # print result  print("The list after conversion : " + str(res))  

Output
The original dictionary : {'Nikhil': (22, 'JIIT'), 'Akshat': (21, 'JIIT')} The list after conversion : [('Nikhil', 22, 'JIIT'), ('Akshat', 21, 'JIIT')]

Time complexity: O(n), where n is the number of items in the dictionary
Auxiliary space: O(n), where n is the number of items in the dictionary.

Method #2: Using list comprehension + items() + "+" operator This method is similar to the above function with the modification of the above method and allows the functionality to add as many possible keys rather than restricted number that were allowed by the above method. 

Python3
# Python3 code to demonstrate  # Dictionary to list of tuple conversion  # using list comprehension + items() + "+" operator   # initializing Dictionary  test_dict = {"Nikhil" : (22, "JIIT"), "Akshat" : (21, "JIIT")}   # printing original dictionary  print("The original dictionary : " + str(test_dict))   # using list comprehension + items() + "+" operator  # Dictionary to list of tuple conversion  res = [(key, ) + val for key, val in test_dict.items()]   # print result  print("The list after conversion : " + str(res))  

Output
The original dictionary : {'Nikhil': (22, 'JIIT'), 'Akshat': (21, 'JIIT')} The list after conversion : [('Nikhil', 22, 'JIIT'), ('Akshat', 21, 'JIIT')]

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary.

Method #3: Using map() and items()
This method uses the map function to map the keys and values of the dictionary to tuples and then convert the map object to a list using the built-in list() function.

Python3
#Python3 code to demonstrate #Dictionary to list of tuple conversion #using map() and items() #initializing Dictionary test_dict = {"Nikhil" : (22, "JIIT"), "Akshat" : (21, "JIIT")}  #printing original dictionary print("The original dictionary : " + str(test_dict))  #using map() and items() #Dictionary to list of tuple conversion res = list(map(lambda t: (t[0], )+t[1], test_dict.items()))  #print result print("The list after conversion : " + str(res)) #This code is contributed by Edula Vinay Kumar Reddy 

Output
The original dictionary : {'Nikhil': (22, 'JIIT'), 'Akshat': (21, 'JIIT')} The list after conversion : [('Nikhil', 22, 'JIIT'), ('Akshat', 21, 'JIIT')]

Time complexity: O(n)
Auxiliary Space : O(n)

Method #4 : Using keys(),values(),extend(),tuple() methods

Python3
# Python3 code to demonstrate # Dictionary to list of tuple conversion   # initializing Dictionary test_dict = {"Nikhil" : (22, "JIIT"), "Akshat" : (21, "JIIT")}  # printing original dictionary print("The original dictionary : " + str(test_dict))   # Dictionary to list of tuple conversion res=[] x=list(test_dict.keys()) y=list(test_dict.values()) for i in range(0,len(x)):     b=[x[i]]     b.extend(list(y[i]))     res.append(tuple(b))      # print result print("The list after conversion : " + str(res)) 

Output
The original dictionary : {'Nikhil': (22, 'JIIT'), 'Akshat': (21, 'JIIT')} The list after conversion : [('Nikhil', 22, 'JIIT'), ('Akshat', 21, 'JIIT')]

Time Complexity : O(N)
Auxiliary Space : O(N)

Method 5 : using a for loop and the append() method 

step-by-step approach

  1. Create a dictionary named test_dict with two key-value pairs.
    Create an empty list named res.
    Use a for loop to iterate through each key-value pair in test_dict.
    For each key-value pair, create a new tuple named tuple_val that consists of the key and the values of the tuple in the dictionary. To concatenate the key and the tuple values into a single tuple, you use the + operator. The key is enclosed in a tuple to make it a single-element tuple.
    Append the tuple_val tuple to the res list using the append() method.
    After all key-value pairs have been processed, print the res list.
     
Python3
test_dict = {"Nikhil": (22, "JIIT"), "Akshat": (21, "JIIT")}  # using a for loop and the append() method res = [] for k, v in test_dict.items():     tuple_val = (k,) + v     res.append(tuple_val)  print("The list after conversion : " + str(res)) 

Output
The list after conversion : [('Nikhil', 22, 'JIIT'), ('Akshat', 21, 'JIIT')]

The time complexity of this approach is O(n), where n is the number of key-value pairs in the dictionary 
space complexity is also O(n), since it creates a new list of tuples with n elements.


Next Article
Convert List of Dictionary to Tuple list Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
  • Python dictionary-programs
Practice Tags :
  • python

Similar Reads

  • Python | List of tuples to dictionary conversion
    Interconversions are always required while coding in Python, also because of the expansion of Python as a prime language in the field of Data Science. This article discusses yet another problem that converts to dictionary and assigns keys as 1st element of tuple and rest as it's value. Let's discuss
    3 min read
  • Convert Dictionary to List of Tuples - Python
    Converting a dictionary into a list of tuples involves transforming each key-value pair into a tuple, where the key is the first element and the corresponding value is the second. For example, given a dictionary d = {'a': 1, 'b': 2, 'c': 3}, the expected output after conversion is [('a', 1), ('b', 2
    3 min read
  • Convert List of Dictionary to Tuple list Python
    Given a list of dictionaries, write a Python code to convert the list of dictionaries into a list of tuples.Examples: Input: [{'a':[1, 2, 3], 'b':[4, 5, 6]}, {'c':[7, 8, 9], 'd':[10, 11, 12]}] Output: [('b', 4, 5, 6), ('a', 1, 2, 3), ('d', 10, 11, 12), ('c', 7, 8, 9)] Below are various methods to co
    5 min read
  • Python | Type conversion of dictionary items
    The interconversion of data types is quite common, and we may have this problem while working with dictionaries as well. We might have a key and corresponding list with numeric alphabets, and we with to transform the whole dictionary to integers rather than string numerics. Let's discuss certain way
    6 min read
  • Convert List of Named Tuples to Dictionary - Python
    We are given a list of named tuples we need to convert it into dictionary. For example, given a list li = [d("ojaswi"), d("priyank"), d("sireesha")], the goal is to convert it into a dictionary where each unique key maps to a list of its corresponding values, like {'Name': 'ojaswi'},{'Name': 'priyan
    3 min read
  • Python Convert Dictionary to List of Values
    Python has different types of built-in data structures to manage your data. A list is a collection of ordered items, whereas a dictionary is a key-value pair data. Both of them are unique in their own way. In this article, the dictionary is converted into a list of values in Python using various con
    3 min read
  • Python | Tuple key dictionary conversion
    Interconversions are always required while coding in Python, also because of expansion of Python as a prime language in the field of Data Science. This article discusses yet another problem that converts to dictionary and assigns keys as first pair elements as tuple and rest as it’s value. Let’s dis
    5 min read
  • Convert List of Tuples to Dictionary Value Lists - Python
    The task is to convert a list of tuples into a dictionary where the first element of each tuple serves as the key and the second element becomes the value. If a key appears multiple times in the list, its values should be grouped together in a list. For example, given the list li = [(1, 'gfg'), (1,
    4 min read
  • Convert Tuples to Dictionary - Python
    The task is to convert a list of tuples into a dictionary where each tuple contains two element . The first element of each tuple becomes the key and the second element becomes the value. If a key appears multiple times its values should be grouped together, typically in a list. For example, given t
    4 min read
  • Convert List of Lists to Dictionary - Python
    We are given list of lists we need to convert it to python . For example we are given a list of lists a = [["a", 1], ["b", 2], ["c", 3]] we need to convert the list in dictionary so that the output becomes {'a': 1, 'b': 2, 'c': 3}. Using Dictionary ComprehensionUsing dictionary comprehension, we ite
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences