Open In App
Related Articles

Python | Reverse Order Sort in String List

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Sometimes, while working with Python, we can have a problem in which we need to perform the reverse sort operation in all the Strings that are present in a list. This problem can occur in general programming and web development. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using list comprehension + sorted() + join() + reverse This is one way in which this problem can be solved. In this, we use sorted() functionality to perform sort operation and join() is used to reconstruct the string list. The reverse logic is implemented by passing “reverse” as True parameter to sorted(). 

Python3

# Python3 code to demonstrate working of
# Reverse Order Sort in String List
# using list comprehension + sorted() + join() + reverse
 
# initialize list
test_list = ['gfg', 'is', 'good']
 
# printing original list
print("The original list : " + str(test_list))
 
# Reverse Order Sort in String List
# using list comprehension + sorted() + join() + reverse
res = [''.join(sorted(ele, reverse = True)) for ele in test_list]
 
# printing result
print("List after string reverse sorting : " + str(res))

                    
Output : 
The original list : ['gfg', 'is', 'good']
List after string reverse sorting : ['ggf', 'si', 'oogd']

Time complexity: O(n*nlogn), where n is the length of the test_list. The list comprehension + sorted() + join() + reverse takes O(n*nlogn) time
Auxiliary Space: O(n), extra space of size n is required

Method #2 : Using map() + sorted() + reverse + join() + lambda The combination of above method can also be used to perform this task. In this, we perform the functionality of traversal using map() and lambda rather than list comprehension. The reverse logic is implemented by passing “reverse” as True parameter to sorted(). 

Python3

# Python3 code to demonstrate working of
# Reverse Order Sort in String List
# using map() + sorted() + join() + lambda + reverse
 
# initialize list
test_list = ['gfg', 'is', 'good']
 
# printing original list
print("The original list : " + str(test_list))
 
# Reverse Order Sort in String List? + reverse
# using map() + sorted() + join() + lambda
res = list(map(lambda ele: "".join(sorted(ele, reverse = True)), test_list))
 
# printing result
print("List after string reverse sorting : " + str(res))

                    
Output : 
The original list : ['gfg', 'is', 'good']
List after string reverse sorting : ['ggf', 'si', 'oogd']

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 a for loop and sorting the strings in reverse order

Python3

# Python3 code to demonstrate working of
# Reverse Order Sort in String List
# using a for loop + sorted()
 
# initialize list
test_list = ['gfg', 'is', 'good']
 
# printing original list
print("The original list : " + str(test_list))
 
# Reverse Order Sort in String List? + reverse
# using a for loop + sorted()
res = []
for ele in test_list:
    sorted_str = ''.join(sorted(ele, reverse=True))
    res.append(sorted_str)
 
# printing result
print("List after string reverse sorting : " + str(res))

                    

Output
The original list : ['gfg', 'is', 'good']
List after string reverse sorting : ['ggf', 'si', 'oogd']

Time complexity: O(n * m log m), where n is the number of elements in the list and m is the maximum length of a string in the list. Sorting takes O(m log m) time and it is done for each string in the list.
Auxiliary space: O(n * m), since we are creating a new list of sorted strings.



Last Updated : 03 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments