Convert Tuple Value List to List of Tuples – Python
Last Updated : 21 Jan, 2025
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.
Python 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))
OutputConverted tuple list: [('Gfg', 5), ('Gfg', 6), ('is', 5), ('best', 7)]
Let’s explore other methods to achieve the same:
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.
Python 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))
OutputConverted 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.
Python 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))
OutputConverted 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.
Python 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))
OutputThe 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 key k
to each tuple t
in the list v
. 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 final res
list.
Similar Reads
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, ),
4 min read
Python - Convert List of Lists to Tuple of Tuples
Sometimes, while working with Python data, we can have a problem in which we need to perform interconversion of data types. This kind of problem can occur in domains in which we need to get data in particular formats such as Machine Learning. Let us discuss certain ways in which this task can be per
8 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
Python | Convert list of tuples to list of list
Converting list of tuples to list of lists in Python is a task where each tuple is transformed into list while preserving its elements. This operation is commonly used when we need to modify or work with the data in list format instead of tuples. Using numpyNumPy makes it easy to convert a list of t
3 min read
Python - Convert List to Single valued Lists in Tuple
Conversion of data types is the most common problem across CS domain nowdays. One such problem can be converting List elements to single values lists in tuples. This can have application in data preprocessing domain. Let's discuss certain ways in which this task can be performed. Input : test_list =
7 min read
Convert List of Tuples to List of Strings - Python
The task is to convert a list of tuples where each tuple contains individual characters, into a list of strings by concatenating the characters in each tuple. This involves taking each tuple, joining its elements into a single string, and creating a new list containing these strings. For example, gi
3 min read
Python | Convert list of tuples into list
In Python we often need to convert a list of tuples into a flat list, especially when we work with datasets or nested structures. In this article, we will explore various methods to Convert a list of tuples into a list. Using itertools.chain() itertools.chain() is the most efficient way to flatten a
3 min read
Convert Set of Tuples to a List of Lists in Python
Sets and lists are two basic data structures in programming that have distinct uses. It is sometimes necessary to transform a collection of tuples into a list of lists. Each tuple is converted into a list throughout this procedure, and these lists are subsequently compiled into a single, bigger list
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 strings to list of tuples in Python
Sometimes we deal with different types of data types and we require to inter-convert from one data type to another hence interconversion is always a useful tool to have knowledge. This article deals with the converse case. Let's discuss certain ways in which this can be done in Python. Method 1: Con
5 min read