Python List Sort

python-list--reverse-sorting-ipcisco.com-1

List are one of the most important data types used in python. We use python lists for various reasons. But sometimes these lists are mixed and we need to sort it. Here, we will learn Python List Sort. We use python sort () function to sort Python Lists alphabetically. The default behavior of this function is ascending (A to Z or Lower to higher value). We can also use it to sort as descending.

 

In the below example, firstly, we will sort a list consist of different numbers.

 

list1 = [123, 28, 53, 5, 17, 80, 72]
list1.sort()
print(list1)

 

The output of this Python List Sort code will be like below:

 

[5, 17, 28, 53, 72, 80, 123]

 

python-list--reverse-sorting-ipcisco.com-1

 

You can watch also the video of this lesson!

As you can see above, the list is sorted as ascending alphabetically. This is the default behaviour.

 

To sort a python list as descending, we will use reverse parameter of sort () method. The default behavior of this parameter is false, so the sort function sorts as ascending. To convert this as descending, we will set reverse parameter as True.

 

To do this, we will use the same list as above. This time we will also set reverese parameter as True.

 

list1 = [123, 28, 53, 5, 17, 80, 72]
list1.sort(reverse=True)
print(list1)

 

The output list will be like below sorted as descending alphabetically.

 

[123, 80, 72, 53, 28, 17, 5]

 

python-list-sorting-ipcisco.com-1

We can use Python List Sort for also string lists. Some lists consist of different strings and if we sould like to sort this strings in the list, we can use python sort () function.

 

list1 = ["cat", "dog", "bird", "lion", "bear"]
list1.sort()
print(list1)

 

The output of this Python List Sort code will be like below. The strings are sorted as ascending.

 

['bear', 'bird', 'cat', 'dog', 'lion']

 

If we have a string list consist of strings that begin with upper and lower cases, the sorting will be different because of the alphanumeric values location. For example, in the below example, you can see the sorted list that seems wrong if you think as case-insenstive.

 

list1 = ["cat", "Dog", "bird", "lion", "bear"]
list1.sort()
print(list1)

 

The output of this List Sorting in Python will be:

 

['Dog', 'bear', 'bird', 'cat', 'lion']

 

To sort the list correctly as case-insenstive, we can use str.lower function as key function. ,

 

list1 = ["cat", "Dog", "bird", "lion", "bear"]
list1.sort(key = str.lower)
print(list1)

 

The output of this python code will be like below. The sort process will as case-insenstive.

 

['bear', 'bird', 'cat', 'Dog', 'lion']

  python-list-sorting-ipcisco.com-3

There is also another way of reverse sorting. If you would like to sort your list reverse independant from the alphabet, you can do this with python reverse () function. In other words, python reverse method do not sort alphabetically, instead, it sorts the lists in reverse order.

 

list1 = ["cat", "dog", "bird", "lion", "bear"]
list1.reverse()
print(list1)

 

As you can see below, the output of this python reverse function code will be:

 

['bear', 'lion', 'bird', 'dog', 'cat']

 

python-list-sorting-reverse-ipcisco.com-1

Here, the sorting is as reverse of the list. This is not related about the location of the characters in the alphabet.

 

In this lesson, we have learned how to sort a python list with different Python List Sorting examples. You can use similar codes in your python codes for various purposes. List Sorting is an important function used in python coding. So, you will see different usages of these function during your career.

 

Back to: Python Programming Course > Python Lists

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k