Sort a Python List - sort() and sorted() method

Introduction

Sorting a Python List means arranging all the elements in a certain order. The order may be ascending or descending order. 

In this tutorial, you'll learn how to sort a list in python. There are two built-in methods to do so, sort() and sorted() methods. We'll use both of them here.

sort() vs sorted() method in python

Both are used for sorting list elements. The main difference is sorted() method only shows the sorted value of a list. It doesn't change the order of the main data, but the sort() method sorts the list values permanently.

Parameters

reverse (Optional) - By default sort() and sorted() method sort list elements in ascending order. Passing the "reverse=True" parameter to both functions will sort the list elements in descending order.

key (optional) - To specify the sorting criteria, need to pass a function here.

sorted() method


languages = ['Python', 'C++', 'Java', 'Ruby', 'C#', 'PHP', 'HTML']

print("Original List:\n",languages)

# Sorting the list using the sorted() method
print("\nData in ascending order:\n", sorted(languages))

# Checking the list if it has been sorted or not
print("\nNow the List is:\n", languages)

Output

Original List:
 ['Python', 'C++', 'Java', 'Ruby', 'C#', 'PHP', 'HTML']

Data in ascending order:
 ['C#', 'C++', 'HTML', 'Java', 'PHP', 'Python', 'Ruby']

Now the List is:
 ['Python', 'C++', 'Java', 'Ruby', 'C#', 'PHP', 'HTML']

As you can see, the sorted() method only shows the values in a sorted manner without modifying the original data.

sorted() method for descending order

Sorting list elements in descending order is so simple. We just need to pass an additional parameter "reverse=True" to the sorted() function.


languages = ['Python', 'C++', 'Java', 'Ruby', 'C#', 'PHP', 'HTML']

# Sorting in Descending order using the sorted method
print("Data in descending order:\n", sorted(languages, reverse=True))

Output

Data in descending order:
 ['Ruby', 'Python', 'PHP', 'Java', 'HTML', 'C++', 'C#']

sort() method

As I said earlier, the sort() method sort list elements permanently in ascending order. See the example below.


lang = ['Python', 'C++', 'Java', 'Ruby', 'C#', 'PHP', 'HTML']

print("Original List:\n",lang)

# Sorting the list using the sort() method
lang.sort()

# Sorting in ascending order
print("\nSorted List:\n",lang)

Output

Original List:
 ['Python', 'C++', 'Java', 'Ruby', 'C#', 'PHP', 'HTML']

Sorted List:
 ['C#', 'C++', 'HTML', 'Java', 'PHP', 'Python', 'Ruby']

sort() method for descending order


lang = ['Python', 'C++', 'Java', 'Ruby', 'C#', 'PHP', 'HTML']

# Sorting in Descending order using the sort() method
lang.sort(reverse=True)
print("Data in descending order:\n",lang)

Output

Data in descending order:
 ['Ruby', 'Python', 'PHP', 'Java', 'HTML', 'C++', 'C#']

Sorting list without key parameter

Let's start this topic with an example. Look at the example below.

lang = [('Python', 'C++'), ('Java', 'Ruby'), ('C#', 'PHP'), ('HTML', 'Kotlin')]

This is a list containing several tuples and each of them contains two unique programming language names. The real agenda is that, if we try to sort this list in general, the data will be sorted based on the first item of each tuple in that list. Let's try it practically.


lang = [('Python', 'C++'), ('Java', 'Ruby'), ('C#', 'PHP'), ('HTML', 'Kotlin')]

# Sorting in Descending order using the sort() method
lang.sort()

print("Sorted Data:\n",lang)

Output

Sorted Data:
 [('C#', 'PHP'), ('HTML', 'Kotlin'), ('Java', 'Ruby'), ('Python', 'C++')]

As you can see, the data has been sorted based on the first item of each tuple in that list. Suppose you want to sort the same list but based on the second item of each tuple, then, the method we followed just before is not gonna work. The next section is the solution to this problem.

Sorting list with the key parameter

In this case, we will define a separate function and pass it as a key parameter in the sort() function. Let's see what happens.


def sortSecond(items):
return items[1]

lang = [('Python', 'C++'), ('Java', 'Ruby'), ('C#', 'PHP'), ('HTML', 'Kotlin')]

# Sorting in Descending order using the sort() method
lang.sort(key=sortSecond)

print("Sorted Data:\n",lang)

Output

Sorted Data:
 [('Python', 'C++'), ('HTML', 'Kotlin'), ('C#', 'PHP'), ('Java', 'Ruby')]

See, we got exactly what we wanted.

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post