Open In App

Python – Sort List items on basis of their Digits

Last Updated : 10 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given List of elements, perform sorting on basis of digits of numbers.

Input : test_list = [434, 211, 12, 3] Output : [12, 211, 3, 434] Explanation : 3 < 12, still later in list, as initial digit, 1 < 3. Hence sorted by digits rather than number. Input : test_list = [534, 211, 12, 7] Output : [12, 211, 534, 7] Explanation : 7 < 534, still later in list, as initial digit, 5 < 7. Hence sorted by digits rather than number.

Method #1 : Using map() + str() + ljust() + sorted()

In this, the list elements are converted into string using map() and str(), ljust() is used to append constant character to make each number equal length. Then sort is performed using sorted().

Python3




# Python3 code to demonstrate working of
# Sort List by Digits
# Using map() + str() + ljust() + sorted()
 
# initializing list
test_list = [434, 211, 12, 54, 3]
 
# printing original list
print("The original list is : " + str(test_list))
 
# converting elements to string
temp1 = map(str, test_list)
 
# getting max length
max_len = max(map(len, temp1))
 
# performing sort operation
res = sorted(test_list, key = lambda idx : (str(idx).ljust(max_len, 'a')))
 
# printing result
print("List after sorting : " + str(res))


Output

The original list is : [434, 211, 12, 54, 3]
List after sorting : [12, 211, 3, 434, 54]

Time Complexity: O(nlogn) where n is the number of elements in the list “test_list”. 
Auxiliary Space: O(1), additional space is not needed.

Method #2 : Using list comprehension + sorted() + lambda

In this, we as parameter to sorted(),  pass a list of digits. Performing sort according to that gives desired results.

Python3




# Python3 code to demonstrate working of
# Sort List by Digits
# Using list comprehension + sorted() + lambda
 
# initializing list
test_list = [434, 211, 12, 54, 3]
 
# printing original list
print("The original list is : " + str(test_list))
 
# performing sort operation
# converting number to list of Digits
res = sorted(test_list, key = lambda ele: [int(j) for j in str(ele)])
 
# printing result
print("List after sorting : " + str(res))


Output

The original list is : [434, 211, 12, 54, 3]
List after sorting : [12, 211, 3, 434, 54]


Similar Reads

Python | Sort tuple list on basis of difference of elements
Sometimes, while working with data, we can have a problem of sorting them. There are many types of basis on which sorting can be performed. But this article discusses sorting on basis of difference of both elements of pair. Let's discuss certain ways in which this can be done. Method #1 : Using sorted() + lambda The combination of above functionali
5 min read
Python | Sort list of numbers by sum of their digits
Given a list of non-negative numbers, the task is to sort these integers according to the sum of their digits. Examples: Input : [12, 10, 106, 31, 15] Output : [10, 12, 31, 15, 106] Input : [1111, 19, 29, 11, 12, 9] Output : [11, 12, 1111, 9, 19, 29] Let's discuss few Pythonic ways to do this task. Approach #1 : List comprehension Using for loop to
4 min read
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 &lt; is &lt; ks &lt; t [elements from rear], hence the order. Input : test_list = ["gfg", "is", "best"] Output : ['gfg', 'is', 'best'] Explanation : g &lt; s &lt; t [elements from
5 min read
Python Program to find the profit or loss when CP of N items is equal to SP of M items
Given [Tex]N   [/Tex]and [Tex]M   [/Tex]denoting that the Cost Price of N articles is equal to the Selling Price of M articles. The task is to determine the profit or Loss percentage. Examples:  Input: N = 8, M = 9 Output: Loss = -11.11% Input: N = 8, M = 5 Output: Profit = 60% Formula:-  Below is the implementation of the above approach: C/C++ Cod
1 min read
Python | Sorting URL on basis of Top Level Domain
Given a list of URL, the task is to sort the URL in the list based on the top-level domain. A top-level domain (TLD) is one of the domains at the highest level in the hierarchical Domain Name System of the Internet. Example - org, com, edu. This is mostly used in a case where we have to scrap the pages and sort URL according to top-level domain. It
3 min read
Python - Create nested list containing values as the count of list items
Given a list, the task is to write a Python program to create a nested list where the values are the count of list items. Examples: Input: [1, 2, 3] Output: [[1], [2, 2], [3, 3, 3]] Input: [4, 5] Output: [[1, 1, 1, 1], [2, 2, 2, 2, 2]] Method 1: Using nested list comprehension The list will contain the count of the list items for each element e in
2 min read
Python program to Sort a List of Dictionaries by the Sum of their Values
Given Dictionary List, sort by summation of their values. Input : test_list = [{1 : 3, 4 : 5, 3 : 5}, {1 : 100}, {8 : 9, 7 : 3}] Output : [{8: 9, 7: 3}, {1: 3, 4: 5, 3: 5}, {1: 100}] Explanation : 12 &lt; 13 &lt; 100, sorted by values sum Input : test_list = [{1 : 100}, {8 : 9, 7 : 3}] Output : [{8: 9, 7: 3}, {1: 100}] Explanation : 12 &lt; 100, so
6 min read
Python | Sort the items alphabetically from given dictionary
Given a dictionary, write a Python program to get the alphabetically sorted items from given dictionary and print it. Let’s see some ways we can do this task. Code #1: Using dict.items() C/C++ Code # Python program to sort the items alphabetically from given dictionary # initialising _dictionary dict = {'key2' : 'For', 'key3': 'IsGeeks', 'key1' : '
1 min read
Sort a list in Python without sort Function
Python Lists are a type of data structure that is mutable in nature. This means that we can modify the elements in the list. We can sort a list in Python using the inbuilt list sort() function. But in this article, we will learn how we can sort a list in a particular order without using the list sort() method. Sort a List Without Using Sort Functio
3 min read
Python | Convert an array to an ordinary list with the same items
When working with arrays, a frequent requirement is to convert them into standard lists, while retaining the original elements. This article delves into the art of effortlessly transforming arrays into lists using Python, maintaining the integrity of their contents. Input: array('i', [1, 3, 5, 3, 7, 1, 9, 3])Output: [1, 3, 5, 3, 7, 1, 9, 3]Explanat
4 min read
Practice Tags :