VOOZH about

URL: https://www.geeksforgeeks.org/python/python-nested-list-to-single-value-tuple/

⇱ Python - Nested List to single value Tuple - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python - Nested List to single value Tuple

Last Updated : 15 Jul, 2025

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, )]

Python Nested List to Single Value Tuple

Below are the ways by which we can convert nested lists to single-value tuples:

  • Using list comprehension ( For single nesting )
  • Using isinstance() + recursion
  • Using extend() method
  • Using Two nested loop
  • Using map and chain functions
  • Using reduce() function and add operator
  • Using NumPy

Flatten Nested List to Single-Value Tuples with List Comprehension

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.


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)
Auxiliary space: O(n)

Recursively Convert Nested List to Single-Value Tuples Using isinstance()

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.


Output
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)

Convert Nested List to Single-Value Tuples Using extend() Method

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.


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)
Auxiliary space: O(n) 

Convert Nested List to Single-Value Tuples with Two Nested Loops

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.


Output
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)

Nested List to Single Value Tuple Using map and chain functions

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).


Output
[(5,), (6,), (4,), (7,), (10,), (12,), (9,), (11,)]

Time complexity: O(n)
Auxiliary Space: O(n)

Python Convert Nested List to Tuple Using the reduce() and add Operator

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))


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^2)
Auxiliary Space: O(n)

Python Nested List to Single Value Tuple Using NumPy

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.

Comment