# Python – Sort on basis of reverse Strings

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

 `# Python3 code to demonstrate working of  ` `# Sort on basis of reverse Strings ` `# Using reverse() + sort() ` ` `  `# initializing list ` `test_list ``=` `[``"gfg"``, ``"is"``, ``"best"``, ``"for"``, ``"geeks"``] ` ` `  `# printing original list ` `print``(``"The original list : "` `+` `str``(test_list)) ` ` `  `# reverse() to reverse each string  ` `res ``=` `[] ` `for` `ele ``in` `test_list: ` `    ``res.append("".join(``reversed``(ele)))  ` `     `  `# sorting to get required ordering  ` `res.sort() ` ` `  `# reverse each element again ` `test_list ``=` `[] ` `for` `ele ``in` `res: ` `    ``test_list.append("".join(``reversed``(ele)))  ` `     `  `# printing result  ` `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']
```

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

 `# Python3 code to demonstrate working of  ` `# Sort on basis of reverse Strings ` `# Using list slicing + sort() ` ` `  `# initializing list ` `test_list ``=` `[``"gfg"``, ``"is"``, ``"best"``, ``"for"``, ``"geeks"``] ` ` `  `# printing original list ` `print``(``"The original list : "` `+` `str``(test_list)) ` ` `  `# [::-1] to reverse each string  ` `# sort() to sort ` `test_list.sort(key ``=` `lambda` `sub: sub[::``-``1``]) ` ` `  `# printing result  ` `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']
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.