Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python Sort List

The Python’s List sort() method sorts the elements in ascending/descending/user-defined order.

Python Sort List

The following are the various techniques to sort the elements:

  • Sort the list in ascending order
  • Sort the list in descending order
  • Sort the list using user-defined order
  • Sort a List of Objects
  • Sorting of a list using a key

1. Sorting List Elements in Ascending Order

The sort() function is used to sort the elements of the list in ascending order.

input = [1.2, 221, 0.025, 0.124, 1.2]

print(f'Before sorting of elements: {input}')

input.sort()

print(f'After sorting of elements: {input}')

Output:

Before sorting of elements: [1.2, 221, 0.025, 0.124, 1.2]
After sorting of elements: [0.025, 0.124, 1.2, 1.2, 221]

2. Sorting List Elements in Descending Order

The reverse parameter is used to sort the list elements in the descending order.

Syntax: list-name.sort(reverse=True)

input = [8, 1, 12, 0]

input.sort(reverse = True)

print(input)

Output:

[12, 8, 1, 0]

3. Python Sort List using a key function

Python provides the sorting of elements of the list using a key function as a parameter. Based on the output of the key function, the list would be sorted.

# takes third element for sort
def third_element(x):
    return x[2]


input = [(2, 2, 1), (3, 4, 9), (4, 1, 0), (1, 3, 7)]

# sort list with key
input.sort(key=third_element)

# prints sorted list
print('Sorted list:', input)

Output:

Sorted list: [(4, 1, 0), (2, 2, 1), (1, 3, 7), (3, 4, 9)]

4. Sort the list using user-defined order

# takes third element for sort
def third_element(x):
    return x[2]


input = [(2, 2, 1), (3, 4, 9), (4, 1, 0), (1, 3, 7)]

# sorts list with key in ascending order
input.sort(key=third_element)

# prints sorted list
print('Sorted list in ascending order:', input)

# sorts list with key in descending order
input.sort(key=third_element, reverse=True)

print('Sorted list in descending order:', input)

Output:

Sorted list in ascending order: [(4, 1, 0), (2, 2, 1), (1, 3, 7), (3, 4, 9)]
Sorted list in descending order: [(3, 4, 9), (1, 3, 7), (2, 2, 1), (4, 1, 0)]

5. Sorting a List of Objects

In order to sort the list of custom objects using sort() function, we need to specify the key function specifying the object’s field to achieve the same.

class Details:

    def __init__(self, name, num):
        self.name = name
        self.num = num


    def __str__(self):
        return f'Details[{self.name}:{self.num}]'


    __repr__ = __str__


D1 = Details('Safa', 12)
D2 = Details('Aman', 1)
D3 = Details('Shalini', 45)
D4 = Details('Ruh', 30)

input_list = [D1, D2, D3, D4]

print(f'Before Sorting: {input_list}')



def sort_by_num(details):
    return details.num


input_list.sort(key=sort_by_num)
print(f'After Sorting By Number: {input_list}')

Output:

Before Sorting: [Details[Safa:12], Details[Aman:1], Details[Shalini:45], Details[Ruh:30]]
After Sorting By Number: [Details[Aman:1], Details[Safa:12], Details[Ruh:30], Details[Shalini:45]]

Conclusion

Thus, we have understood various techniques to sort elements in a list.


References

Leave a Reply

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