![]() |
VOOZH | about |
Sometimes, while working with Python Tuples, we can have a problem in which we need to perform flattening of tuples, which can be nested and undesired. This can have application across many domains such as Data Science and web development. Let's discuss certain way in which this task can be performed.
Input : test_tuple = ((4, 7), ((4, 5), ((6, 7), (7, 6)))) Output : ((4, 7), (4, 5), (6, 7), (7, 6)) Input : test_tuple = ((4, 7), (5, 7), (1, 3)) Output : ((4, 7), (5, 7), (1, 3))
Method 1: Using recursion + isinstance() The combination of above functionalities can help us achieve solution to this problem. In this we use recursion to perform the task of digging into each tuple for inner tuples, and for decision of flattening, isinstance() is used depending upon tuple container or primitive data.
Step-by-step approach :
The original tuple : ((4, 5), ((4, 7), (8, 9), (10, 11)), ((9, 10), (3, 4))) The flattened tuple : ((4, 5), (4, 7), (8, 9), (10, 11), (9, 10), (3, 4))
Time complexity: The time complexity of the function is O(N), where N is the total number of elements in the input nested tuple.
Auxiliary space: The auxiliary space used by the function is also O(N), where N is the total number of elements in the input nested tuple
Method 2: itertools.chain.from_iterable()
This program uses itertools.chain.from_iterable() method to flatten the nested tuple test_tuple and returns a new flattened tuple.
The original tuple : ((4, 5), ((4, 7), (8, 9), (10, 11)), ((9, 10), (3, 4))) The flattened tuple : (4, 5, (4, 7), (8, 9), (10, 11), (9, 10), (3, 4))
Time complexity: O(n), where n is the total number of elements in the nested tuple.
Space complexity: O(n), since the output tuple contains n elements.
Method 3: use a generator function that iterates over the elements of the input tuple and yields non-tuple values, and recursively calls itself on tuple values.
Here's how the flatten_tuples function works:
The original tuple : ((4, 5), ((4, 7), (8, 9), (10, 11)), ((9, 10), (3, 4))) The flattened tuple : (4, 5, 4, 7, 8, 9, 10, 11, 9, 10, 3, 4)
This approach has a time complexity of O(n), where n is the total number of elements in the input tuple, since each element is visited once.
The auxiliary space is O(d), where d is the depth of the nested tuples, since the recursion depth is bounded by the depth of the nested tuples.
Method 4: using the reduce() function from the functools module
The original tuple : ((4, 5), ((4, 7), (8, 9), (10, 11)), ((9, 10), (3, 4))) The flattened tuple : (4, 5, 4, 7, 8, 9, 10, 11, 9, 10, 3, 4)
Time Complexity: O(n), where n is the total number of elements in the nested tuple, as each element is visited exactly once.
Auxiliary Space: O(n), as the maximum depth of recursion is equal to the maximum nesting level of the tuple.
Method 5: Using a stack
The original tuple : ((4, 5), ((4, 7), (8, 9), (10, 11)), ((9, 10), (3, 4))) The flattened tuple : (4, 3, 10, 9, 11, 10, 9, 8, 7, 4, 5, 4)
Time complexity: O(n) - where n is the total number of elements in the nested tuple.
Auxiliary space: O(n) - where n is the total number of elements in the nested tuple.