Related Articles

# Sort numeric strings in a list in Python

• Last Updated : 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 Method
In 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 sorting``for` `i ``in` `range``(``0``, ``len``(test_list)) :``    ``test_list[i] ``=` `int``(test_list[i])``test_list.sort()`` ` `# printing result``print` `(``"The resultant sorted list  : "` `+`  `str``(test_list))`
Output:
```The original list is : ['4', '6', '7', '2', '1']
The resultant sorted list  : [1, 2, 4, 6, 7]
```

Method #2 : Using `sort()` using key
The 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 sorting``test_list.sort(key ``=` `int``)`` ` `# printing result``print` `(``"The resultant sorted list  : "` `+`  `str``(test_list))`
Output:

```The original list is : ['4', '6', '7', '2', '1']
The resultant sorted list  : ['1', '2', '4', '6', '7']
```

Method #3 : Using `sorted()` + key
This 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 sorting``res ``=` `sorted``(test_list, key ``=` `int``)`` ` `# printing result``print` `(``"The resultant sorted list  : "` `+`  `str``(res))`
Output:
```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 sorting``res ``=` `sorted` `(test_list, key ``=` `lambda` `x: (``len` `(x), x))`` ` `# printing result``print` `(``"The resultant sorted list  : "` `+`  `str` `(res))`
Output:
```The original list is : ['4', '6', '7', '2', '1']
The resultant sorted list  : ['1', '2', '4', '6', '7']
```

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