Python | Flatten and Reverse Sort Matrix
Last Updated :
18 May, 2023
The flattening of list of list has been discussed many times, but sometimes, in addition to flattening, it is also required to get the string in reverse sorted manner. Let’s discuss certain ways in which this can be done.
Method #1: Using sorted() + reverse + list comprehension This idea is similar to flattening a list of list but in addition to it, we add a sorted function along with reverse as key, to reverse sort the returned flattened list done by list comprehension.
Python3
test_list = [[ 3 , 5 ], [ 7 , 3 , 9 ], [ 1 , 12 ]]
print ( "The original list : " + str (test_list))
res = sorted ([j for i in test_list for j in i], reverse = True )
print ( "The reverse sorted and flattened list : " + str (res))
|
Output
The original list : [[3, 5], [7, 3, 9], [1, 12]]
The reverse sorted and flattened list : [12, 9, 7, 5, 3, 3, 1]
Time Complexity: O(nlogn), where n is the length of the input list. This is because we’re using the sorted() + reverse + list comprehension which has a time complexity of O(nlogn) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #2: Using itertools.chain() + sorted() + reverse The task that was done by list comprehension above can also be performed using the chain function that links elements of list and then sorted function does the task of sorting with the help of reverse for reverse sorting.
Python3
from itertools import chain
test_list = [[ 3 , 5 ], [ 7 , 3 , 9 ], [ 1 , 12 ]]
print ( "The original list : " + str (test_list))
res = sorted (chain( * test_list), reverse = True )
print ( "The reverse sorted and flattened list : " + str (res))
|
Output
The original list : [[3, 5], [7, 3, 9], [1, 12]]
The reverse sorted and flattened list : [12, 9, 7, 5, 3, 3, 1]
Time Complexity: O(n*nlogn), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #3 : Using extend() and sort() methods
Python3
test_list = [[ 3 , 5 ], [ 7 , 3 , 9 ], [ 1 , 12 ]]
print ( "The original list : " + str (test_list))
res = []
for i in test_list:
res.extend(i)
res.sort(reverse = True )
print ( "The reverse sorted and flattened list : " + str (res))
|
Output
The original list : [[3, 5], [7, 3, 9], [1, 12]]
The reverse sorted and flattened list : [12, 9, 7, 5, 3, 3, 1]
Time Complexity: O(nlogn), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #4: Using recursion
Python3
def flatten(lst):
if not lst:
return []
if isinstance (lst[ 0 ], list ):
return flatten(lst[ 0 ]) + flatten(lst[ 1 :])
return lst[: 1 ] + flatten(lst[ 1 :])
test_list = [[ 3 , 5 ], [ 7 , 3 , 9 ], [ 1 , 12 ]]
print ( "The original list : " + str (test_list))
res = sorted (flatten(test_list), reverse = True )
print ( "The reverse sorted and flattened list : " + str (res))
|
Output
The original list : [[3, 5], [7, 3, 9], [1, 12]]
The reverse sorted and flattened list : [12, 9, 7, 5, 3, 3, 1]
Time complexity : O(n log n)
Auxiliary Space: O(n)
Method 5: Use a stack and a while loop.
Step-by-step approach:
- Initialize an empty list flat_list to store the flattened list.
- Initialize a stack stack with the input list lst.
- Use a while loop to iterate through the stack until it becomes empty.
- Pop the last element from the stack and check if it is a list or not using isinstance() function. If it is a list, extend the stack with its elements in reverse order. If it is not a list, append it to flat_list.
- Use sorted() function with the reverse=True parameter to sort the flat_list in reverse order.
- Return the sorted flattened list.
Python3
def flatten(lst):
flat_list = []
stack = [lst]
while stack:
item = stack.pop()
if isinstance (item, list ):
stack.extend(item[:: - 1 ])
else :
flat_list.append(item)
return sorted (flat_list, reverse = True )
test_list = [[ 3 , 5 ], [ 7 , 3 , 9 ], [ 1 , 12 ]]
print ( "The original list : " + str (test_list))
res = flatten(test_list)
print ( "The reverse sorted and flattened list : " + str (res))
|
Output
The original list : [[3, 5], [7, 3, 9], [1, 12]]
The reverse sorted and flattened list : [12, 9, 7, 5, 3, 3, 1]
Time complexity: O(n log n), where n is the total number of elements in the input list lst. The sorted() function takes O(n log n) time to sort the flattened list in reverse order.
Auxiliary space: O(n), where n is the total number of elements in the input list lst.
Method 6: Using a generator function and heapq
Python3
import heapq
def flatten_and_sort(lst):
def flatten_generator(lst):
for item in lst:
if isinstance (item, list ):
yield from flatten_generator(item)
else :
yield item
flat_list = list (flatten_generator(lst))
heapq.heapify(flat_list)
sorted_list = []
while flat_list:
sorted_list.append(heapq.heappop(flat_list))
return sorted_list[:: - 1 ]
test_list = [[ 3 , 5 ], [ 7 , 3 , 9 ], [ 1 , 12 ]]
print ( "The original list: " + str (test_list))
res = flatten_and_sort(test_list)
print ( "The reverse sorted and flattened list: " + str (res))
|
Output
The original list: [[3, 5], [7, 3, 9], [1, 12]]
The reverse sorted and flattened list: [12, 9, 7, 5, 3, 3, 1]
Time complexity is O(n + n + n log n) = O(n log n).
The auxiliary space is O(n) because we create a flat list to store all the elements before sorting them.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...