Open In App

Python program to Flatten Nested List to Tuple List

Last Updated : 07 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a list of tuples with each tuple wrapped around multiple lists, our task is to write a Python program to flatten the container to a list of tuples.

Input : test_list = [[[(4, 6)]], [[[(7, 4)]]], [[[[(10, 3)]]]]]
Output : [(4, 6), (7, 4), (10, 3)]
Explanation : The surrounded lists are omitted around each tuple.

Input : test_list = [[[(4, 6)]], [[[(7, 4)]]]]
Output : [(4, 6), (7, 4)]
Explanation : The surrounded lists are omitted around each tuple.

Method #1 : Using recursion + isinstance()

In this, the container wrapping is tested to be list using isinstance(). The recursion strategy is used to check for repeated flattening of the list till tuple. 

step-by-step approach of the given program:

  1. Define an empty list res outside the function remove_lists(). This list will be used to store the flattened elements of the input list.
  2. Define the function remove_lists(test_list) that takes a list as input. This function will recursively flatten the list.
  3. Iterate over the elements of test_list using a for loop.
  4. Check if the current element is a list or not using the isinstance() function. If it is a list, call the remove_lists() function recursively with the current element as input. This will flatten the nested list.
  5. If the current element is not a list, append it to the res list.
  6. Return the res list.
  7. Initialize the input list test_list with some nested tuples.
  8. Print the original list test_list.
  9. Call the function remove_lists() with test_list as input. This will flatten the nested list recursively and store the flattened elements in the res list.
  10. Print the flattened list.
  11. End of the program.

Output
The original list is : [[[(4, 6)]], [[[(7, 4)]]], [[[[(10, 3)]]]]]
The Flattened container : [(4, 6), (7, 4), (10, 3)]

Time Complexity: O(n), where n is the total number of elements in the nested tuple list. 
Auxiliary Space: O(n), where n is the total number of elements in the nested tuple list.

Method #2 : Using yield + recursion

This method performs a similar task using recursion. The generator is used to process intermediate results using yield keyword.


Output
The original list is : [[[(4, 6)]], [[[(7, 4)]]], [[[[(10, 3)]]]]]
The Flattened container : [(4, 6), (7, 4), (10, 3)]

Time complexity: O(n), where n is the total number of elements in the input list.
Auxiliary space: O(m), where m is the maximum depth of nested lists in the input list.

Method #3 : Using replace(),split(),list(),map(),tuple() methods


Output
The original list is : [[[(4, 6)]], [[[(7, 4)]]], [[[[(10, 3)]]]]]
The Flattened container : [(4, 6), (7, 4), (10, 3)]

Time Complexity : O(N)
Auxiliary Space : O(N)

Method #4 : Using type() and recursion


Output
The original list is : [[[(4, 6)]], [[[(7, 4)]]], [[[[(10, 3)]]]]]
The Flattened container : [(4, 6), (7, 4), (10, 3)]

Time Complexity : O(N)
Auxiliary Space : O(N)

Method #5: Using itertools.chain() and recursion

Use the itertools.chain() function to flatten a nested list by recursively iterating over its elements using a generator function (flatten_list()) with the yield from statement. The remove_lists() function then applies itertools.chain.from_iterable() to the output of flatten_list() and returns a list containing all the flattened elements.

STEPS:

  1. First, we import the itertools module.
  2. Next, we define a function called flatten_list(lst) which takes a list as input and flattens it. This function uses a recursive approach to flatten nested lists. It checks whether the current item is a list or not. If it is a list, it calls the flatten_list() function again with that list as input, otherwise it yields the item.
  3. We define another function called remove_lists(test_list) which takes a nested list as input and returns a flattened list. This function uses the chain.from_iterable() method from itertools to concatenate all the nested lists into a single flattened list. It also calls the flatten_list() function to flatten the nested lists.
  4. We initialize a nested list called test_list which contains tuples.
  5. We print the original nested list.
  6. We call the remove_lists() function with test_list as input and store the flattened list in a variable called res.
  7. We print the flattened list.

Output
The original list is : [[[(4, 6)]], [[[(7, 4)]]], [[[[(10, 3)]]]]]
The Flattened container : [4, 6, 7, 4, 10, 3]

Time complexity: O(n), where n is the total number of elements in the input list.
Auxiliary space: O(n), as the flatten_list() function uses recursion to iterate over the elements of the list, and each recursive call creates a new stack frame with local variables.

Method #6: Using stack and iteration

Step-by-step approach:

  • Create an empty stack and append the input list test_list to it.
  • Create an empty list res.
  • While the stack is not empty:
    a. Pop the top element ele from the stack.
    b. If ele is a list, append its elements to the stack.
    c. If ele is not a list, append it to res.
  • Return res.

Below is the implementation of the above approach:


Output
The original list is : [[[(4, 6)]], [[[(7, 4)]]], [[[[(10, 3)]]]]]
The Flattened container : [(10, 3), (7, 4), (4, 6)]

Time complexity: O(n), where n is the total number of elements in the input list.
Auxiliary space: O(n), where n is the total number of elements in the input list.


Next Article

Similar Reads