How to sort a list in Python?

Python list data type can hold multiple different types of values in a single variable. It is a collection of ordered and changeable data items.

The given example shows a list in Python.

list1=['Python', 'programming', 2021]

A list can have unsorted elements to sort them in a specific ascending or descending order Python provides two built-in methods.

  • sort() – It changes the list and doesn’t return a value.
  • sorted() – This function creates a copy of the original list and then sorts its elements it doesn’t change the original list items, it returns the sorted list.

In this article, we will discuss how to sort a list in Python along with some examples.

How to sort a list in ascending order

The syntax for using the Python sort() method is given below.

list_name.sort(reverse=True|False, key=myFunc)

The sort() method can have given optional parameters.

  • reverse – If it is True the list will be sorted in descending order
  • key – This is the function that serves as a key for the sort comparison

For example –

In this example, there are two lists i.e. numbers and fruits. We will sort these lists in ascending order by using the sort() method.

numbers=[5, 3, 9, 6, 1, 0]
fruits =['orange', 'apple', 'grapes', 'cherry']

# sorting given lists
numbers.sort()
fruits.sort()

# displaying sorted lists
print(numbers)
print(fruits)

When you execute this program it will produce the given result.

How to sort a list in descending order

If you pass reverse=True in the sort method, it will sort the given list in reverse or descending order. Now see the example that is given below.

numbers=[5, 3, 9, 6, 1, 0]
fruits =['orange', 'apple', 'grapes', 'cherry']

# sorting given lists in descending order
numbers.sort(reverse=True)
fruits.sort(reverse=True)

# displaying sorted lists
print(numbers)
print(fruits)

When you execute this code you will see lists get sorted in descending order.

How to sort a list in a user-defined order

In this, we will use the second optional parameter i.e. key of the sort method to sort a list in a user-defined order.

By default, a tuple is sorted by using its first parameter. Now you will see the example of how to customize the sort method to sort it using the second element.

#Python program to sort a list using key
def sortSecond(val):
return val[1]

list1 = [(1, 2), (3, 3), (2,0), (1, 1)]

# Sort list in ascending order
list1.sort(key = sortSecond)
print(list1)

# Sort list in descending order
list1.sort(key = sortSecond, reverse = True)
print(list1)

When you execute this program it will produce.

As you can see in the output which is given above the list is sorted in ascending and descending order respectively according to the second element of the tuple.

Another example shows the details of employees of an organization we will sort them according to given criteria using the sort method and custom key.

# Python program to sorting a list using custom key
employees = [
    {'Name': 'Ajay', 'age': 25, 'salary': 12000},
    {'Name': 'Dinesh Chandra', 'age': 22, 'salary': 8000},
    {'Name': 'John', 'age': 26, 'salary': 10000},
    {'Name': 'Nikhita', 'age': 21, 'salary': 12000},
]

# sort by name in ascending order
employees.sort(key=lambda x: x.get('Name'))
print(employees, end='\n\n')

# sort by Age in ascending order
employees.sort(key=lambda x: x.get('age'))
print(employees, end='\n\n')

# sort by salary in descending order
employees.sort(key=lambda x: x.get('salary'), reverse=True)
print(employees, end='\n\n')

When you execute this It will display the lists sorted according to name, age in ascending order, and salary in descending order. We use the Python lambda function in this program.

Now see the output of this in the given image –

Conclusion

Now you have learned how to sort a list in Python. If you have a query then write us in the comments below.

Leave a Comment