Python - Nested List to single value Tuple


In the given problem statement we have to convert the given nested list into a single value tuple with the help of Python functionalities. So we will use different techniques to solve this task in python.

Understanding the Problem

The problem at hand is to create a single value tuple using the nested list and implement the code in Python. Sometimes we need to solve these kinds of problems in competitive programming or in company interviews. The nested list allows us to store multiple sublists in a single list. And a single value tuple means in a tuple there should be only a single value. So basically we have to create an algorithm to convert the nested list into a single tuple value.

Logic for The Above Problem

The given problem can be solved using different approaches. But in our article we will solve this problem in three ways. First approach will be using a reduce function. Second approach will be using a loop. And in the third approach we will use the numpy library of Python.

Algorithm - Using Reduce function

  • Step 1 − First we will import the reduce function from the functools library of Python.

  • Step 2 − After that we will define the function as convert_the_list and pass a parameter as the_list which is the nested list.

  • Step 3 − Now we will use reduce function from functools for flattening the given nested list.

  • Step 4 − Then we will create a new list for creating a tuple for each value in the given list.

  • Step 5 − At the end we will return the Output of a single tuple value in the list.

Example - Using Reduce function

# Import the reduce function
from functools import reduce
#Function to convert nested list in single tuple value
def convert_the_list(the_list):
   single_list = reduce(lambda a, b: a+b, the_list)
   result = [(a,) for a in single_list]
   return result

#define the nested list
nested_list = [[12, 20], [15, 17, 16], [36], [45, 87]]

# Show the input nested list
print("Input Nested list : " + str(nested_list))

Output = convert_the_list(nested_list)

# Print the result
print("Single tuple value list: " + str(Output))

Output

Input Nested list : [[12, 20], [15, 17, 16], [36], [45, 87]]
Single tuple value list: [(12,), (20,), (15,), (17,), (16,), (36,), (45,), (87,)]

Complexity

The code using the reduce function for converting the nested list into a single value tuple is O(n²), here n is the total number of items in the given nested list. As we are using the reduce function which has O(n²) time complexity, which is the cause of this time complexity.

Algorithm - Using for loop

  • Step 1 − Define the function called convert_the_list and pass an argument the_list which represents the input nested list.

  • Step 2 − Initialize a blank object called the_tuple to store the single value tuple.

  • Step 3 − Now using the for loop we will traverse the items of the nested list and also using the nested loop to traverse the nested list items.

  • Step 4 − During the iteration process we will append each item in the_tuple object.

  • Step 5 − And return the value of the_tuple to get the single value tuple list.

Example - Using for loop

#function to convert nested list in single value tuple
def convert_the_list(the_list):
   the_tuple = []
   for sub_list in the_list:
      for val in sub_list:
         the_tuple.append((val,))
   return the_tuple
#Initialize the nested lists
nested_list = [[1, 5, 6], [4, 8, 9]]
Output = convert_the_list(nested_list)
print("The Single value tuple:", Output)

nested_list1 = [[12, 14, 16, 18]]
Output1 = convert_the_list(nested_list1)
print("The Single value tuple:", Output1)

Output

The Single value tuple: [(1,), (5,), (6,), (4,), (8,), (9,)]
The Single value tuple: [(12,), (14,), (16,), (18,)]

Complexity

The time complexity for converting the nested list in a single value tuple list using the for loop is O(m * n), here m is the counts of the sublist in the input list and n is the total counts of the items in the list.

Algorithm - Using Numpy

  • Step 1 − First, Import the numpy library as nmp using the import keyword.

  • Step 2 − Define the function as convert_the_list and pass an argument as the_list which is the nested input list.

  • Step 3 − Now first we will convert the given list into an array using numpy inside the function.

  • Step 4 − And then we will flatten the given array and remove the subarrays using the flatten method.

  • Step 5 − As we have the flattened array now we will convert it into the tuple using the tuple function. And return the Output_tuple to show in the Output.

Example - Using Numpy

# Import the numpy library
import numpy as nmp

def convert_the_list(the_list):
   # convert the list into an array
   the_array = nmp.array(the_list)
   # Flatten the above array
   flat_array = the_array.flatten()
   # convert the flattened array into the tuple format
   Output_tuple = tuple(flat_array)
   return Output_tuple

# Initialize the input nested list
the_nested_list = [[14, 17, 13], [11, 18, 15], [12, 19, 16]]
the_result = convert_the_list(the_nested_list)
print("The converted single tuple: ", the_result)

Output

The converted single tuple:  (14, 17, 13, 11, 18, 15, 12, 19, 16)

Complexity

The time complexity for converting the nested list into a single tuple value using numpy library is O(n) as we are first converting the list into an array of size n and then again converting the array into the tuple.

Conclusion

So we have successfully solved the given problem of changing the nested list into a single value tuple using Python. As we have used three approaches to do the given problem. All the code has different time complexity. And we have learned the usage of reduce function, for loops and numpy library functions.

Updated on: 17-Oct-2023

36 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements