How to sort a list in python?

Sorting refers to arranging the data in a particular order. Sorting a list of strings alphabetically or a list of numbers. Let us consider a list num_list = [2, 5, 1, 3, 4]. The num_list when sorted is updated to [1, 2, 3, 4, 5].

The different methods to sort the list are as follows.

Sorting the list using the sort() function

Python supports the inbuilt sort() function to sort the list. sort() function sorts the list in ascending order. The sort() function doesn't return anything. The two optional parameters for the sort() function are key and reverse.

The syntax for the sort() function is as follows.

#syntax:
list.sort(key = ..., reverse =...)
  • The parameter reverse takes a boolean value. If the reverse is set to True it sorts the list in descending order.
  • The parameter key takes a function as a value for user-defined comparison.

Example 1: Let us consider a list num_list = [3, 5, 8, 4.5]. The sort() function is used to arrange the list in ascending order.

num_list = [3, 5, 8, 4.5]
print("num_list = ", num_list)
num_list.sort()
print("The num_list is updated to", num_list)

The output for the sorted list in ascending order.

num_list =  [3, 5, 8, 4.5]
The num_list is updated to [3, 4.5, 5, 8]

Example 2: Arranging the list in descending order by using parameter reverse = True.

num_list = [3, 5, 8, 4.5]
print("num_list = ", num_list)
num_list.sort(reverse =  True)
print("The num_list is updated to", num_list)

The output for num_list = [3, 5, 8, 4.5] in descending order.

num_list =  [3, 5, 8, 4.5]
The num_list is updated to [8, 5, 4.5, 3]

Sorting the list using the sorted() function

sorted() function is a built-in function in python. The major difference between the sort() and sorted() function is, sorted() function returns a sorted list and doesn't modify the original list.

The syntax for the sorted() function is as follows.

#syntax:
sorted(list, key=..., reverse=...)
  • The parameter reverse takes a boolean value. If the reverse is set to True it sorts the list in descending order.
  • The parameter key takes a function as a value for user-defined comparison.

The key and reverse are optional parameters in the sorted() function.

Example 1: Let us consider a list num_list = [3, 5, 8, 4.5]. The sorted() function is used to arrange the list in ascending order and return a sorted list.

num_list = [3, 5, 8, 4.5]
print("num_list = ", num_list)
sort_list = sorted(num_list)
print("The num_list is updated to", sort_list)

The output for the sorted list in ascending order.

num_list =  [3, 5, 8, 4.5]
The num_list is updated to [3, 4.5, 5, 8]

Sorting the list using the for loop

Let us consider a list num_list = [3, 5, 8, 4.5]. Two for loops are used to repeatedly swap the elements in each pass if they are not in ascending order.

num_list = [3, 5, 8, 4.5]
print("num_list = ", num_list)
for i in range(len(num_list)):
    for j in range(i+1, len(num_list)):
        if num_list[i] > num_list[j]:
            num_list[i] , num_list[j] = num_list[j] , num_list[i]
print("The num_list is updated to: ",num_list)

The output for the sorted list is given below.

num_list =  [3, 5, 8, 4.5]
The num_list is updated to:  [3, 4.5, 5, 8]
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions