Python | Dictionary to list of tuple conversion
Last Updated :
22 Apr, 2023
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))
OutputThe 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))
OutputThe 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
OutputThe 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))
OutputThe 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
- 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))
OutputThe 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.
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
2 min read