Convert Tuple Value List to List of Tuples - Python
We are given a dictionary with values as a list of tuples and our task is to convert it into a list of tuples where each tuple consists of a key and its corresponding value. Note: Each key will appear with each value from the list of tuples. For example: We have a dictionary dict = {'Gfg' : [(5, ), (6, )], 'is' : [(5, )], 'best' :[(7, )]} then output will be [('Gfg', 5), ('Gfg', 6), ('is', 5), ('best', 7)]
Using *
Operator ( unpacking operator)
In this method we iterate through the dictionary and use the unpacking *
operator to extract values from the tuples and map them to their respective keys.
d = {'Gfg' : [(5, ), (6, )], 'is' : [(5, )], 'best' :[(7, )]}
# using items() to extract all items and pair key with tuple values
res = []
for k, v in d.items():
for ele in v:
res.append((k, *ele))
print("Converted tuple list: " + str(res))
Output
Converted tuple list: [('Gfg', 5), ('Gfg', 6), ('is', 5), ('best', 7)]
Let's explore other methods to achieve the same:
Table of Content
Using List Comprehension + * Operator
This method is quite similar to using *
Operator ( unpacking operator) method but the only difference is that we get a one-liner solution using list comprehension.
d = {'Gfg' : [(5, ), (6, )], 'is' : [(5, )], 'best' :[(7, )]}
# list comprehension to pair key with tuple values
res = [(k, *ele) for k, v in d.items() for ele in v]
print("Converted tuple list: " + str(res))
Output
Converted tuple list: [('Gfg', 5), ('Gfg', 6), ('is', 5), ('best', 7)]
Using map() and lambda Function
map()
function with a lambda is used to transform each key-value pair from the dictionary. For each key-value pair, it creates a list of tuples where the key is prepended to the unpacked tuple values and at last the nested list is flattened using list comprehension.
d = {'Gfg': [(5, ), (6, )], 'is': [(5, )], 'best':[(7, )]}
# map and lambda to pair key with tuple values
res = list(map(lambda x: [(x[0], *y) for y in x[1]], d.items()))
res = [item for sublist in res for item in sublist]
print("Converted tuple list: " + str(res))
Output
Converted tuple list: [('Gfg', 5), ('Gfg', 6), ('is', 5), ('best', 7)]
Explanation: lambda x: [(x[0], *y) for y in x[1]]
takes the key (x[0]
) and prepends it to each tuple in the value list (x[1]
) and the *
operator unpacks the tuple into separate elements.
Using a dictionary comprehension
This method uses dictionary comprehension to first create a new dictionary where each key is prepended to its corresponding tuples then it flattens the result into a single list using a list comprehension.
d = {'Gfg': [(5, 6, 7), (1, 3), (6, )],
'is': [(5, 5, 2, 2, 6)],
'best': [(7,), (9, 16)]}
# using dictionary comprehension to prepend key to each tuple
p_dict = {k: [(k, *t) for t in v] for k, v in d.items()}
# flattening the result
res = [i for sublist in p_dict.values() for i in sublist]
print("The converted tuple list : " + str(res))
Output
The converted tuple list : [('Gfg', 5, 6, 7), ('Gfg', 1, 3), ('Gfg', 6), ('is', 5, 5, 2, 2, 6), ('best', 7), ('best', 9, 16)]
Explanation:
{k: [(k, *t) for t in v] for k, v in d.items()}
this line creates a list of tuples where each tuple is formed by prepending the keyk
to each tuplet
in the listv
. and the*
operator is used to unpack the tuples.[item for sublist in prepended_dict.values() for item in sublist]
this list comprehension flattens the result by iterating over the values of the newly created dictionary (pre_dict
) which are lists of tuples and adding each tuple to the finalres
list.