Python Sort() Function

Introduction

In this article we will explore how to efficiently use Python's sort() list function. Using the sorted() function will teach you a different approach to sorting data in Python and show you how it differs from sort (). By the conclusion, you will be able to adapt the sorting to suit your needs and understand the fundamentals of sorting a list in Python.

Understanding the Sort() Function

One approach for sorting a list in Python is the sort() method. Using sort(), you can instantly sort a list. This indicates that the original list is changed immediately.
Syntax:

list_name.sort(reverse=..., key=... )

  • The list you're dealing with is called "list_name."
  • A list can be sorted and modified using sort().
  • List items are sorted either in ascending or descending order. The sort() method takes two optional arguments. The first optional argument is reverse. It designates the list's order of sorting, either ascending or descending. It accepts a Boolean value, which can either be True or False.
  • List items are arranged in ascending order when the default setting of False is used. The list is sorted backwards and in decreasing order if it is set to True. The second optional argument is crucial.
  • Any specific sort() criterion you may have must be specified using a function or method.
  • Since the sort() function only adjusts the initial list, it returns None, which implies there is no return value. It doesn't provide you with a new list.

Sorting in Ascending Order

# a list of numbers
my_numbers = [ 33 ,22, 7, 100, 11,10, 8, 3, 54]
#sort list in-place in ascending order
my_numbers.sort()
#print modified list
print(my_numbers)


#output
#[3, 7, 8, 10, 11, 22, 33, 54, 100]

As seen above, the order of the integers is smallest to largest.

Sorting in Reverse or Descending Order

Use the optional reverse argument together with the sort() function, setting its value to True, to organize the elements in a list in descending order. syntax: list name.sort(reverse=True). Let's think about sorting strings this time rather than numbers.

# a list of strings
programming_languages = ["Python", "Swift","Java", "C++", "Go", "Rust"]

#sort list in-place in  reverse alphabetical order
programming_languages.sort(reverse=True)

#print modified list
print(programming_languages)

#output : ['Swift', 'Rust', 'Python', 'Java', 'Go', 'C++']


The order of the list elements has now been reversed.

Custom Sorting

  1. The key parameter enables you to execute more specialized sorting procedures. The key parameter's supplied value must be a callable object.
  2. A callable object may be invoked and referenced just like a method or function, since it can be called.
  3. In order to describe the reasoning for the sorting criterion, this method or function set to key will be applied to each element in the list before any sorting takes place.
  4. The built-in len() function, for instance, is assigned to the key parameter to sort a list depending on the length of its members.
  5. By using the len () method, each member in the list will have its length determined by the number of characters it contains.

programming_languages = ["Python", "Swift","Java", "C++", "Go", "Rust"]

programming_languages.sort(key=len)

print(programming_languages)


#output
#['Go', 'C++', 'Java', 'Rust', 'Swift', 'Python']

As in the last example, strings are sorted this time according to their length rather than the standard ascending order. The left-hand side has the shortest string, while the right-hand side has the longest string. You may also mix the reverse and key parameters. For instance, you may arrange the list items in descending order, depending on length.

programming_languages = ["Python", "Swift","Java", "C++", "Go", "Rust"]

programming_languages.sort(key=len, reverse=True)

print(programming_languages)

#output

#['Python', 'Swift', 'Java', 'Rust', 'C++', 'Go']

The Differences between sort() and sorted()

Similar to how the sorted() function operates, the sort() technique also works. The sorted() function's general syntax is as follows:

sorted(list_name,reverse=...,key=...)

  1. Built-in function sorted() takes an iterable. Following that, it arranges it either in ascending or descending order.
  2. Three arguments are accepted by sorted(). Two parameters are optional, while one is mandatory. The necessary argument is the list name.
  3. List is the parameter in this case, but any other iterable object is acceptable for sorted().
  4. The same optional arguments that the sort() function allows are likewise accepted by the sorted() method: reverse and key.
  5. The primary distinction between the sort() and sorted() functions is that the latter accepts a list and produces a fresh, sorted version of it.

That's it for this article. You now understand how to use Python's sort() function to order a list. We also looked at the main distinctions between using sort() to sort a list and using the sorted() function.

write your code here: Coding Playground