Python – Convert Tuple value list to List of tuples

Given a dictionary with values as tuple list, convert to key mapped list of tuples.

Input : test_dict = {‘Gfg’ : [(5, 6, 7), (6, )], ‘is’ : [(5, 5, 2, 2, 6)], ‘best’ :[(7, )]}
Output : [(‘Gfg’, 5, 6, 7), (‘Gfg’, 6), (‘is’, 5, 5, 2, 2, 6), (‘best’, 7)]
Explanation : Keys grouped with values.

Input : test_dict = {‘Gfg’ : [(5, ), (6, )], ‘is’ : [(5, )], ‘best’ :[(7, )]}
Output : [(‘Gfg’, 5), (‘Gfg’, 6), (‘is’, 5), (‘best’, 7)]
Explanation : Keys grouped with values.

Method #1 : Using loop + * operator + items() 

This is one of the ways in which this task can be performed. In this we iterate for all the keys using loop and map keys with all values by unpacking all values in tuple using * operator.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert Tuple value list to List of tuples
# Using loop + * operator + items()
  
# initializing dictionary
test_dict = {'Gfg' : [(5, 6, 7), (1, 3), (6, )],
             'is' : [(5, 5, 2, 2, 6)], 
             'best' :[(7, ), (9, 16)]}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# using items() to extract all the items of 
# dictionary
res = []
for key, val in test_dict.items():
    for ele in val:
        res.append((key, *ele))
  
# printing result 
print("The converted tuple list : " + str(res)) 

chevron_right


Output

The original dictionary is : {‘Gfg’: [(5, 6, 7), (1, 3), (6, )], ‘is’: [(5, 5, 2, 2, 6)], ‘best’: [(7, ), (9, 16)]}
The converted tuple list : [(‘Gfg’, 5, 6, 7), (‘Gfg’, 1, 3), (‘Gfg’, 6), (‘is’, 5, 5, 2, 2, 6), (‘best’, 7), (‘best’, 9, 16)]

Method #2 : Using list comprehension + * operator + items()

This is yet another way to solve this problem. Solves in similar way as above method. Only difference being offers one-liner solution using list comprehension.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert Tuple value list to List of tuples
# Using list comprehension + * operator + items()
  
# initializing dictionary
test_dict = {'Gfg' : [(5, 6, 7), (1, 3), (6, )],
             'is' : [(5, 5, 2, 2, 6)], 
             'best' :[(7, ), (9, 16)]}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# list comprehension encapsulates whole logic 
# into one line
res = [(key, *ele) for key, sub in test_dict.items() for ele in sub]
  
# printing result 
print("The converted tuple list : " + str(res)) 

chevron_right


Output

The original dictionary is : {‘Gfg’: [(5, 6, 7), (1, 3), (6, )], ‘is’: [(5, 5, 2, 2, 6)], ‘best’: [(7, ), (9, 16)]}
The converted tuple list : [(‘Gfg’, 5, 6, 7), (‘Gfg’, 1, 3), (‘Gfg’, 6), (‘is’, 5, 5, 2, 2, 6), (‘best’, 7), (‘best’, 9, 16)]




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.