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
test_dict = { "Nikhil" : ( 22 , "JIIT" ), "Akshat" : ( 21 , "JIIT" )}
print ( "The original dictionary : " + str (test_dict))
res = [(key, i, j) for key, (i, j) in test_dict.items()]
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
test_dict = { "Nikhil" : ( 22 , "JIIT" ), "Akshat" : ( 21 , "JIIT" )}
print ( "The original dictionary : " + str (test_dict))
res = [(key, ) + val for key, val in test_dict.items()]
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
test_dict = { "Nikhil" : ( 22 , "JIIT" ), "Akshat" : ( 21 , "JIIT" )}
print ( "The original dictionary : " + str (test_dict))
res = list ( map ( lambda t: (t[ 0 ], ) + t[ 1 ], test_dict.items()))
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 #4 : Using keys(),values(),extend(),tuple() methods
Python3
test_dict = { "Nikhil" : ( 22 , "JIIT" ), "Akshat" : ( 21 , "JIIT" )}
print ( "The original dictionary : " + str (test_dict))
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 ( "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
- 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" )}
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...