How to sort the objects in a list in Python?


In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array. Square brackets are used to denote it, and a comma (,) is used to divide two items in the list.

In this article, we will show you how to sort the objects and elements in a list using python. Below are the different methods to accomplish this task −

  • Using sort() method

  • Using sorted() method

Assume we have taken a list containing some elements. We will return the list elements after sorting either in ascending or descending order.

NOTE − If the object/element of the list is a string the elements are sorted in alphabetical order.

Method 1: Using sort() method

The sort() method sorts the original list in place. It signifies that the sort() method changes the order of the list's elements

By default, the sort() method uses the less-than operator (<) to sort the entries of a list i.e, in ascending order. In other words, it prioritizes lesser elements above higher ones.

To sort elements from highest to lowest(descending order), use the reverse=True parameter in the sort() method.

list.sort(reverse=True)

Example 1

The following program sorts the list elements in ascending and descending order using the sort() method −

# input list lst = [10, 4, 12, 1, 9, 5] # sorting elements of the list in ascending order lst.sort() print("Sorting list items in ascending order: ", lst) # sorting elements of the list in descending order lst.sort(reverse=True) print("Sorting list items in descending order: ", lst)

Output

On executing, the above program will generate the following output −

Sorting list items in ascending order: [1, 4, 5, 9, 10, 12]
Sorting list items in descending order: [12, 10, 9, 5, 4, 1]

In this case, we've given a list of random values. The sort() method was then applied to the list, which sorted the given list in ascending order and printed the list in ascending order. The same list was then sorted in descending order by passing an additional key to the sort() function, reverse=True, and the list was printed in descending order.

Example:2 For a list containing string values

The following program sorts the list elements(string) in ascending and descending order using the sort() method −

# input list lst = ['hello','this','is','tutorials','point','website','welcome','all'] # sorting string elements of the list in ascending order lst.sort() print("Sorting list items in ascending order: ", lst) # sorting string elements of the list in descending order lst.sort(reverse=True) print("Sorting list items in descending order: ", lst)

Output

On executing, the above program will generate the following output −

Sorting list items in ascending order: ['all', 'hello', 'is', 'point', 'this', 'tutorials', 'website', 'welcome']
Sorting list items in descending order: ['welcome', 'website', 'tutorials', 'this', 'point', 'is', 'hello', 'all']

We can see that all the elements are sorted in alphabetical order

Method 2: Using sorted() method

The sorted() function returns a sorted list of the iterable object given.

You can choose between ascending and descending order. Numbers are sorted numerically, while strings are arranged alphabetically.

Syntax

sorted(iterable, key=key, reverse=reverse)

Parameters

iterable − It is a sequence

key − A function that will be executed to determine the order. The default value is None.

reverse − A Boolean expression. True sorts ascending, False sorts descending. The default value is False.

Example

The following program sorts the list elements in ascending and descending order using the sorted() method −

# input list lst = [10, 4, 12, 1, 9, 5] # sorting elements of the list in ascending order print("Sorting list items in ascending order: ", sorted(lst)) # sorting elements of the list in descending order print("Sorting list items in descending order: ", sorted(lst, reverse=True))

Output

On executing, the above program will generate the following output −

Sorting list items in ascending order: [1, 4, 5, 9, 10, 12]
Sorting list items in descending order: [12, 10, 9, 5, 4, 1]

In this example, we've provided a set of random numbers. We gave the list as an argument to the sorted() method, which sorted and printed the given list in ascending order. The same list was then sorted in descending order by giving an additional key, reverse=True, to the sorted() method, and the list was printed in descending order.

Conclusion

In this article, we learned how to sort list objects/elements using the functions i.e sort() and sorted (). We also learned how to use the same functions to sort a list of items in descending order. We also discussed how the list will be sorted if it contains strings as objects.

Updated on: 03-Nov-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements