Skip to content
Related Articles

Related Articles

Python – Sort by Rear Character in Strings List

Improve Article
Save Article
Like Article
  • Last Updated : 28 Jun, 2021

Given a String list, perform sort by the rear character in Strings list.

Input : test_list = [‘gfg’, ‘is’, ‘for’, ‘geeks’] 
Output : [‘gfg’, ‘for’, ‘is’, ‘geeks’] 
Explanation : g < r < s = s, hence the order.

Input : test_list = [‘gfz’, ‘is’, ‘for’, ‘geeks’] 
Output : [‘for’, ‘is’, ‘geeks’, ‘gfz’] 
Explanation : r < s = s < z, hence the order. 

Method #1 : Using sort()

In this, we perform the task of sorting using sort(), and external function is used for the task of getting the rear element in string.

Python3




# Python3 code to demonstrate working of
# Sort by Rear Character in Strings List
# Using sort()
 
# sort key function
def get_rear(sub):
    return sub[-1]
 
# initializing list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# printing original list
print("The original list is : " + str(test_list))
 
# using sort with key fnc.
# performs inplace sort
test_list.sort(key = get_rear)
 
# printing result
print("Sorted List : " + str(test_list))
Output
The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
Sorted List : ['gfg', 'for', 'is', 'geeks', 'best']

Method #2 : Using sorted() + lambda

In this, we use sorted() for performing sort, explicit, and use a lambda function to perform the task of getting the rear element.

Python3




# Python3 code to demonstrate working of
# Sort by Rear Character in Strings List
# Using sorted() + lambda
 
# initializing list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# printing original list
print("The original list is : " + str(test_list))
 
# lambda function for rear element
# performs non-inplace sort
res = sorted(test_list, key = lambda sub : sub[-1])
 
# printing result
print("Sorted List : " + str(res))
Output
The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
Sorted List : ['gfg', 'for', 'is', 'geeks', 'best']

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!