Python – Sort on basis of reverse Strings
Last Updated :
04 May, 2023
Given a String list, sort list on basis of reverse of strings.
Input : test_list = [“gfg”, “is”, “best”, “geeks”] Output : [‘gfg’, ‘is’, ‘geeks’, ‘best’] Explanation : g < is < ks < t [elements from rear], hence the order. Input : test_list = [“gfg”, “is”, “best”] Output : [‘gfg’, ‘is’, ‘best’] Explanation : g < s < t [elements from rear], hence the order.
Method #1 : Using sort() + reverse()
This is one of the ways in which this task can be performed. In this, we first reverse each element, perform sort and then again reverse each string to get resultant ordering.
Python3
test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list : " + str (test_list))
res = []
for ele in test_list:
res.append("".join( reversed (ele)))
res.sort()
test_list = []
for ele in res:
test_list.append("".join( reversed (ele)))
print ( "List after sorting on reversed strings : " + str (test_list))
|
Output
The original list : ['gfg', 'is', 'best', 'for', 'geeks']
List after sorting on reversed strings : ['gfg', 'for', 'is', 'geeks', 'best']
Time Complexity: O(nlogn), where n is the elements of list
Auxiliary Space: O(n), where n is the size of list
Method #2 : Using list slicing + sort()
This is yet another way in which this task can be performed. in this list slicing is used to perform reverse operation and sort() is used to sort, in one liner.
Python3
test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list : " + str (test_list))
test_list.sort(key = lambda sub: sub[:: - 1 ])
print ( "List after sorting on reversed strings : " + str (test_list))
|
Output
The original list : ['gfg', 'is', 'best', 'for', 'geeks']
List after sorting on reversed strings : ['gfg', 'for', 'is', 'geeks', 'best']
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 3 : Using a for loop to iterate through the list and swap adjacent elements based on their reverse string value.
step by step approach.
- Import the itertools module.
- Initialize a list of strings named test_list.
- Print the original list.
- Use a for loop to iterate through each element in the list.
- Inside the for loop, use another for loop to iterate through the remaining elements in the list.
- Compare the reversed form of the current element with the reversed form of the next element using an if statement.
- If the current element is greater than the next element in terms of their reversed form, swap their positions in the list using tuple assignment.
- Print the sorted list based on their reversed form.
Python3
test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list : " + str (test_list))
for i in range ( len (test_list)):
for j in range (i + 1 , len (test_list)):
if test_list[i][:: - 1 ] > test_list[j][:: - 1 ]:
test_list[i], test_list[j] = test_list[j], test_list[i]
print ( "List after sorting on reversed strings : " + str (test_list))
|
Output
The original list : ['gfg', 'is', 'best', 'for', 'geeks']
List after sorting on reversed strings : ['gfg', 'for', 'is', 'geeks', 'best']
Time complexity: O(n^2) where n is the length of the input list.
Auxiliary space: O(1), as we are not using any extra space to store the sorted list.
METHOD 4:Using map() function and reversed() method
APPROACH:.
In this approach, we first reverse each string in the original list using a lambda function and the [::-1] slicing notation. Then, we use the sorted() function to sort the list based on the reversed strings, which we access with a lambda function again. Finally, we extract the original strings by using another lambda function to get the second element in each tuple.
ALGORITHM:
1Define the original list
2.Use map() function and a lambda function to reverse each string in the list
3.Sort the list based on the reversed strings using sorted() function and a lambda function
4.Extract the original strings using another lambda function to get the second element in each tuple
5.Print the sorted list
Python3
original_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
sorted_list = list ( map ( lambda x: x[ 1 ], sorted ([(word[:: - 1 ], word) for word in original_list], reverse = False )))
print ( "List after sorting on reversed strings:" , sorted_list)
|
Output
List after sorting on reversed strings: ['gfg', 'for', 'is', 'geeks', 'best']
Time Complexity:
The time complexity of this approach is O(n log n) because of the sorting operation.
Space Complexity:
The space complexity of this approach is O(n) because we create a list of tuples to store the reversed strings and their original counterparts.
Share your thoughts in the comments
Please Login to comment...