![]() |
VOOZH | about |
Sometimes, while working with Python data, we can have problems in which we need to convert Python Nested lists to single values tuples. This kind of problem can have applications in domains such as web development and competitive programming. Let's discuss certain ways in which this task can be performed.
Example:
Input : test_list = [[5, 6], [4, 7, 10, 17]]
Output : [(5, ), (6, ), (4, ), (7, ), (10, ), (17, )]Input : test_list = [[5, 6, 7, 8]]
Output : [(5, ), (6, ), (7, ), (8, )]
Below are the ways by which we can convert nested lists to single-value tuples:
In this example, a nested list test_list is converted into a flat tuple container res using list comprehension, where each element from the nested lists is encapsulated in a one-value tuple. The result is a flattened representation of the original nested list.
The original list is : [[5, 6], [4, 7, 10], [12], [9, 11]] The converted container : [(5,), (6,), (4,), (7,), (10,), (12,), (9,), (11,)]
Time complexity: O(n)
Auxiliary space: O(n)
The combination of above functions can be used to solve this problem. In this, we perform the task of flattening and conversion using isinstance() and recursion to cater the case of random nesting as well. In this example, the function helper_func employs recursion and isinstance() to traverse the nested list test_list, creating a flattened tuple container res where each individual integer element is encapsulated in a one-value tuple. The result is a flattened representation of the original nested list.
The original list is : [[5, [6]], [4, 7, [10, 45]], [12], [9, 11]] The converted container : [(5,), (6,), (4,), (7,), (10,), (45,), (12,), (9,), (11,)]
Time complexity: O(n)
Auxiliary space: O(n)
In this example, we are using extend() and the nested list test_list is transformed into a flattened tuple container res using a combination of list comprehension and loops. The individual elements from the nested lists are extracted and enclosed in one-value tuples.
The original list is : [[5, 6], [4, 7, 10], [12], [9, 11]] The converted container : [(5,), (6,), (4,), (7,), (10,), (12,), (9,), (11,)]
Time complexity: O(n)
Auxiliary space: O(n)
The method used in the above program is using two nested for loops. The first for loop iterates over the sublists in the nested list, and the second for loop iterates over the values in each sublist. For each value, a single value tuple is created and appended to the single_value_tuple list. Finally, the single_value_tuple list is returned as the result.
Single value tuple: [(5,), (6,), (4,), (7,), (10,), (17,)] Single value tuple: [(5,), (6,), (7,), (8,)]
Time complexity: O(m * n), where m is the number of sublists in the nested list and n is the total number of values in the nested list.
Auxiliary space: O(n)
In this example, the nested list test_list is flattened into a list of one-value tuples using the map function with a lambda expression and the chain function from the itertools module. The result is a flattened representation of the original nested list, and the output is printed using print(result).
[(5,), (6,), (4,), (7,), (10,), (12,), (9,), (11,)]
Time complexity: O(n)
Auxiliary Space: O(n)
In this example, the function nested_list_to_single_value_tuple utilizes the reduce function from the functools module to flatten the nested list test_list. The resulting flat list is then transformed into a list of one-value tuples, providing a flattened representation of the original nested list. The converted container is printed using print("The converted container : " + str(res))
The original list is : [[5, 6], [4, 7, 10], [12], [9, 11]] The converted container : [(5,), (6,), (4,), (7,), (10,), (12,), (9,), (11,)]
Time complexity: O(n^2)
Auxiliary Space: O(n)
In this example, the nested list test_list is flattened using a list comprehension, and each element is then encapsulated in a one-value tuple using another list comprehension.
Output:
The original list is : [[5, 6], [4, 7, 10], [12], [9, 11]]
The converted container : [(5,), (6,), (4,), (7,), (10,), (12,), (9,), (11,)]
Time Complexity : O(n*m), where n is the number of sublists and m is the maximum length of a sublist.
Auxiliary Space :O(nm), as the flattened list and tuple list both have nm elements.