Related Articles
Python – Sort by Units Digit in List
• Last Updated : 11 Oct, 2020

Given a Integer list, sort by unit digits.

Input : test_list = [76, 434, 23, 22342]
Output : [22342, 23, 434, 76]
Explanation : 2 < 3 < 4 < 6, sorted by unit digits.

Input : test_list = [76, 4349, 23, 22342]
Output : [22342, 23, 76, 4349]
Explanation : 2 < 3 < 6 < 9, sorted by unit digits.

Method #1 : Using sort() + str()

In this, we perform sort using sort() and str() is used to convert integers to string, and then sort by last digit.

Python3

 # Python3 code to demonstrate working of# Sort by Units Digit in List# Using sort() + str()  # helpr_fnc to sort    def unit_sort(ele):      # get last element    return str(ele)[-1]    # initializing liststest_list = [76, 434, 23, 22342]  # printing original listsprint("The original list is : " + str(test_list))  # inplace sort by unit digitstest_list.sort(key=unit_sort)  # printing resultprint("The unit sorted list : " + str(test_list))

Output:

The original list is : [76, 434, 23, 22342]
The unit sorted list : [22342, 23, 434, 76]

Method #2 : Using sorted() + lambda + str()

In this, we perform task of sorting using sorted() and lambda function is used to avoid external function call.

Python3

 # Python3 code to demonstrate working of# Sort by Units Digit in List# Using sorted() + lambda + str()  # initializing liststest_list = [76, 434, 23, 22342]  # printing original listsprint("The original list is : " + str(test_list))  # inplace sort by unit digitsres = sorted(test_list, key=lambda sub: str(sub)[-1])  # printing resultprint("The unit sorted list : " + str(res))

Output:

The original list is : [76, 434, 23, 22342]
The unit sorted list : [22342, 23, 434, 76]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up