How To Do Sorting In Python

Hello, Guys in this article we will discuss the sort() and sorted() python methods used for sorting in python.

Sorting is a process of arranging a list of items in a specific order, either ascending or descending. In Python, there are several built-in methods for sorting lists, such as the sort() and sorted() methods.

The sort() method is used to sort a list in place, meaning that the original list is modified and no new list is created. For example, to sort a list of numbers in ascending order, we can use the following code:

# list.sort() method 
numbers = [3, 1, 4, 2, 5]
numbers.sort()
print(numbers)

Output: [1, 2, 3, 4, 5]

The sorted() function, on the other hand, returns a new sorted list from an iterable, without modifying the original list. For example:

#sorted() method 
numbers = [3, 1, 4, 2, 5]
sorted_numbers = sorted(numbers)
print(sorted_numbers)

Output: [1, 2, 3, 4, 5]

Both sort() and sorted() methods can also accept an optional key parameter, which is a function that is used to extract a comparison key from each list element.

For example, to sort a list of strings by their length, we can use the following code:

#example with sorted()
words = ["apple", "banana", "cherry"]
sorted_words = sorted(words, key=len)
print(sorted_words)

Output: [‘apple’, ‘cherry’, ‘banana’]

In addition, both sort() and sorted() methods can also accept an optional reverse parameter, which is a Boolean value that determines the sort order. If the reverse is set to True, the list is sorted in descending order, otherwise, it is sorted in ascending order.

Another sorting method is the ‘sort()’ on the ‘list’ of tuples, which sorts the list of tuples on the basis of any key element, for example:

# with list.sort() with different  key 
list1 = [("item1", 20), ("item2", 15), ("item3", 25)]
list1.sort(key = lambda x: x[1])
print(list1)

Output: [(‘item2’, 15), (‘item1’, 20), (‘item3’, 25)]

In this example, the list of tuples is sorted on the basis of the second element of the tuple, which is the price.

In conclusion, Python provides several built-in methods for sorting lists, such as sort() and sorted(), which can be used to sort lists in ascending or descending order, and also on the basis of any key element. These methods are easy to use and highly customizable, making them a powerful tool for working with lists in Python.

Note: one of the key differences between the normal list. sort() and sorted() is that we can use a list.sort() for only defined list ,  while our sorted() function can accept any iterable.

Some other examples with the key::

For example, let’s say we have a list of tuples that represent employees, with each tuple containing the employee’s name, age, and salary. We can use the “key” argument to sort this list of employees based on their age, using the following code:

employees = [("John", 25, 45000), ("Mike", 32, 55000), ("Jessica", 29, 60000)]
sorted_employees = sorted(employees, key=lambda x: x[1])
print(sorted_employees)

Output: [(‘John’, 25, 45000), (‘Jessica’, 29, 60000), (‘Mike’, 32, 55000)]

n this example, we used a lambda function as the key argument. The lambda function takes a single argument (x) and returns the second element of the tuple (the age). As a result, the list of employees is sorted in ascending order based on their age.

We can also use the key argument to sort the list based on the salary of the employee, as shown in the following example:

employees = [("John", 25, 45000), ("Mike", 32, 55000), ("Jessica", 29, 60000)]
sorted_employees = sorted(employees, key=lambda x: x[2])
print(sorted_employees)

Output: [(‘John’, 25, 45000), (‘Jessica’, 29, 60000), (‘Mike’, 32, 55000)]

In this example, we used a lambda function as the key argument. The lambda function takes a single argument (x) and returns the third element of the tuple (the salary). As a result, the list of employees is sorted in ascending order based on their salary.

You can also use the key argument with the “itemgetter()” function from the operator module:

import operator
employees = [("John", 25, 45000), ("Mike", 32, 55000), ("Jessica", 29, 60000)]
sorted_employees = sorted(employees, key=operator.itemgetter(1))
print(sorted_employees)

Output: [(‘John’, 25, 45000), (‘Jessica’, 29, 60000), (‘Mike’, 32, 55000)]

In this example, we use operator.itemgetter(1) to extract the second element of each tuple (the age) and passed it as the key argument to the sorted() function. As a result, the list of employees is sorted in ascending order based on their age.

It’s worth noting that the key argument can be used in conjunction with other sorting options, such as “reverse” (to sort the list in descending order) and “cmp” (to specify a custom comparison function).

The extract is a key argument for the sorted function in python is a powerful tool that allows you to sort a list of items based on different criteria. This can be done with the use of the lambda function or the item getter function from the operator module, and it can be used in conjunction with other sorting options to achieve the desired output.

Thanks for the read. Comment out for your query and

Submit a Comment

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

Subscribe

Select Categories