How to sort a list in python

Many times you need to sort a python list. Sorting means arranging list elements in a particular order. Order of list elements may be ascending or descending.
Ascending order for a list of strings means arranging elements in alphabetical order. For a list of numeric values, the items are sorted into increasing order of their values.
Descending order for a list of strings means arranging elements in reverse alphabetical order. For a list of numeric values, the items are sorted into decreasing order of their values.


Python provides a couple of methods to sort or order list elements and both will be discussed in this post. Remember that in order to sort a list, it should have all the elements of the same type. This makes sense since it is not possible to compare a string with a numeric value to determine which comes first.
Method 1: Using sort function
List has a sort function. Invoking this function on a list sorts it in ascending order. Remember that this function does not return a new list but sorts the list on which this function is called. Example,

# create lists
list_one = [10, 34, 96, 1]
list_two = ['xgd', 'dfre', 'likj', '2wd']
# print original lists
print("Before sorting: List one = ", list_one)
print("Before sorting: List two = ", list_two)
# sort both lists
list_one.sort()
list_two.sort()
# print sorted lists
print("After sorting: List one = ", list_one)
print("After sorting: List two = ", list_two)

Above program prints

Before sorting: List one = [10, 34, 96, 1]
Before sorting: List two = [‘xgd’, ‘dfre’, ‘likj’, ‘2wd’]
After sorting: List one = [1, 10, 34, 96]
After sorting: List two = [‘2wd’, ‘dfre’, ‘likj’, ‘xgd’]

Note that numeric lists are arranged in the order of their values and for list with string values, the items are arranged in alphabetical order.


Sorting a list in descending order
Descending order for a numeric list means placing the largest element first and the smallest element last while for an alphabetic list, it means the element that appears last alphabetically is placed first.
sort function takes an argument with key as reverse and value as True or False where True indicates reverse sorting and False indicates sorting in ascending order. It is the same as when no argument is provided.
Thus, above example can be modified to sort the lists in descending order as

# create lists
list_one = [10, 34, 96, 1]
list_two = ['xgd', 'dfre', 'likj', '2wd']
# print original lists
print("Before sorting: List one = ", list_one)
print("Before sorting: List two = ", list_two)
# sort both lists
list_one.sort(reverse=True)
list_two.sort(reverse=True)
# print sorted lists
print("After sorting: List one = ", list_one)
print("After sorting: List two = ", list_two)

Descending sort order is evident from the below output

Before sorting: List one = [10, 34, 96, 1]
Before sorting: List two = [‘xgd’, ‘dfre’, ‘likj’, ‘2wd’]
After sorting: List one = [96, 34, 10, 1]
After sorting: List two = [‘xgd’, ‘likj’, ‘dfre’, ‘2wd’]

Sorting on length
It might happen that you want a list of strings to be sorted in the order of the length of its items irrespective of the characters in the items.
That is, you want the list items arranged in the order of their length and not in alphabetic order. sort function provides this feature also using its key argument and its value should be len. Example,

list_two = ['xgd', 'dfre', 'likj', '2wd']
print("Before sorting: List = ", list_two)
list_two.sort(key=len)
print("After sorting: List = ", list_two)

Output will be

Before sorting: List = [‘xgd’, ‘dfre’, ‘likjdx’, ‘2wd’]
After sorting: List = [‘xgd’, ‘2wd’, ‘dfre’, ‘likjdx’]

Note that the string with maximum characters is placed at the end.
Argument value len is applicable only for list of strings. Trying to use it on a list of numbers will result in an error.


Sorting on values
If there is a list with numeric items in string format and it needs to be sorted on their numeric values, then this is also possible with sort function.
Use the key argument with the value as int and it will sort the list on values. Example,

list_one = ['10', '342', '96', '1']
print("Before sorting: List = ", list_one)
# default sort 
list_one.sort()
print("After default sorting: List = ", list_one)
# sort on values
list_one.sort(key=int)
print("After sorting on values: List = ", list_one)

Output of this code will be

Before sorting: List = [’10’, ‘342’, ’96’, ‘1’]
After default sorting: List = [‘1′, ’10’, ‘342’, ’96’]
After sorting on values: List = [‘1′, ’10’, ’96’, ‘342’]

Notice the difference in sorted list without the int argument and with it. Without int, the list is sorted on the basis of first character of each string while with int argument, the list is sorted on the basis of their values.
Also, remember that int argument can be used only with a list of strings which are convertible to numeric format else there will be an error.
Method 2: Using sorted function
sorted function takes a list as argument and returns a new list which is sorted in ascending order of its elements. Original list is not modified as with the sort function which sorts the original list. Example,

list_one = ['10', '342', '96', '1']
list_two = ['xgd', 'dfre', 'likj', '2wd']
print("Before sorting: List one = ", list_one)
print("Before sorting: List two = ", list_two)
print("After sorting: List one = ", sorted(list_one))
print("After sorting: List two = ", sorted(list_two))

Output is

Before sorting: List one = [’10’, ‘342’, ’96’, ‘1’]
Before sorting: List two = [‘xgd’, ‘dfre’, ‘likj’, ‘2wd’]
After sorting: List one = [‘1′, ’10’, ‘342’, ’96’]
After sorting: List two = [‘2wd’, ‘dfre’, ‘likj’, ‘xgd’]


Sorting list in reverse order
With sorted function also, it is possible to sort a list in reverse(or descending) order. This function takes a reverse as the second argument with values as True and False.
If the value is True, the list is sorted in reverse order. With False value, list is sorted in ascending order similar as that when reverse argument is not provided. Example,

list_one = ['10', '342', '96', '1']
list_two = ['xgd', 'dfre', 'likj', '2wd']
print("Before sorting: List one = ", list_one)
print("Before sorting: List two = ", list_two)
print("After sorting: List one = ", sorted(list_one, reverse=True))
print("After sorting: List two = ", sorted(list_two, reverse=True))

Output of above code will show the list sorted in reverse or descending order.

Before sorting: List one = [’10’, ‘342’, ’96’, ‘1’]
Before sorting: List two = [‘xgd’, ‘dfre’, ‘likj’, ‘2wd’]
After sorting: List one = [’96’, ‘342’, ’10’, ‘1’]
After sorting: List two = [‘2wd’, ‘dfre’, ‘likj’, ‘xgd’]

Hope you liked this post. Hit the clap to show your appreciation.

0

Mark Your Impression

Close Menu