Sort numeric strings in a list in Python - GeeksforGeeks

03 Apr, 2020
Sorting list is an easy task and has been dealt with in many situations. With Machine Learning and Data Science emerging, sometimes we can get the data in the format of list of numbers but with string as data type. Generic Sort functions give erroneous result in that case, hence, several other methods have to employed to perform this particular task. Let’s discuss ways in which this is performed.
Method #1 : Naive MethodIn the naive method requires the type conversion of all the elements into integers of the list iterated through a loop. After that generic sort function is employed to perform the task.
# Python3 code to demonstrate # numeric string sorting# using naive method   # initializing list test_list = [ '4', '6', '7', '2', '1']  # printing original list print ("The original list is : " + str(test_list))  # using naive method # numeric string sortingfor i in range(0, len(test_list)) :    test_list[i] = int(test_list[i])test_list.sort()  # printing resultprint ("The resultant sorted list  : " +  str(test_list))
The original list is : ['4', '6', '7', '2', '1']
The resultant sorted list  : [1, 2, 4, 6, 7]

 Method #2 : Using sort() using keyThe generic sort() can be used to perform this particular task, but has to be specified with the key as integer to convert it to integer while performing sort function internally.
# Python3 code to demonstrate # numeric string sorting# using sort() + key  # initializing list test_list = [ '4', '6', '7', '2', '1']  # printing original list print ("The original list is : " + str(test_list))  # using sort() + key# numeric string sortingtest_list.sort(key = int)  # printing resultprint ("The resultant sorted list  : " +  str(test_list))
The original list is : ['4', '6', '7', '2', '1']
The resultant sorted list  : ['1', '2', '4', '6', '7']

 Method #3 : Using sorted() + keyThis function has a similar internal working as the above function. The improvement that this function offers than the above function is that it doesn’t change the order of original list and just returns a view to display, hence useful at situations where order has to be maintained.
# Python3 code to demonstrate # numeric string sorting# using sorted() + key  # initializing list test_list = [ '4', '6', '7', '2', '1']  # printing original list print ("The original list is : " + str(test_list))  # using sorted() + key# numeric string sortingres = sorted(test_list, key = int)  # printing resultprint ("The resultant sorted list  : " +  str(res))
The original list is : ['4', '6', '7', '2', '1']
The resultant sorted list  : ['1', '2', '4', '6', '7']

 Method #4 : Using sorted() + key with len ()This function has a similar internal working as the above function, but in cases of large integer up to 100 times faster compared to using key = int. The improvement that this function offers than the above function is performance. This sort method can also be applied on sort ().
# Python3 code to demonstrate # numeric string sorting# using sorted () + key with (len (x), x)  # initializing list test_list = [ '4', '6', '7', '2', '1']  # printing original list print ("The original list is : " + str (test_list))  # using sorted () + key with (len (x), x)# numeric string sortingres = sorted (test_list, key = lambda x: (len (x), x))  # printing resultprint ("The resultant sorted list  : " +  str (res))
The original list is : ['4', '6', '7', '2', '1']
The resultant sorted list  : ['1', '2', '4', '6', '7']

JosefHope
Python list-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python program to convert a list to string
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary