Thursday, December 26, 2024
Google search engine
HomeLanguagessort() in Python

sort() in Python

The sort function can be used to sort the list in both ascending and descending order. It can be used to sort lists of integers, floating point numbers, strings, and others in Python. Its time complexity is O(NlogN).

Python sort() Syntax

The syntax of the sort() function in Python is as follows.

Syntax: list_name.sort(key=…, reverse=…)

Parameters:

By default, Python sort() doesn’t require any extra parameters and sorts the list in ascending order. However, it has two optional parameters:

  • key:  function that serves as a key for the sort comparison
  • reverse: If true, the list is sorted in descending order.

Return value: The sort() does not return anything but alters the original list according to the passed parameter.

Sort() in Python Examples

A basic example of Python sort() method.

Python3




unsorted_list = [2,4,5,32,6,255,5,42]
unsorted_list.sort()
print("Now it is sorted:", unsorted_list)


Output

Now it is sorted: [2, 4, 5, 5, 6, 32, 42, 255]

In Python, sort() is a built-in method used to sort elements in a list in ascending order. It modifies the original list in place, meaning it reorders the elements directly within the list without creating a new list. The sort() method does not return any value; it simply sorts the list and updates it.

  • Sorting List in Ascending Order
  • Sorting List in Descending Order
  • Sort with custom function using key
  • Difference between sorted() and sort() function in Python

Sorting List in Ascending Order

By default, the sort() in Python sorts a list in ascending order if we do not provide it with any parameters.

Python3




# List of Integers
numbers = [1, 3, 4, 2]
 
# Sorting list of Integers
numbers.sort()
 
print(numbers)
 
# List of Floating point numbers
decimalnumber = [2.01, 2.00, 3.67, 3.28, 1.68]
 
# Sorting list of Floating point numbers
decimalnumber.sort()
 
print(decimalnumber)
 
# List of strings
words = ["Geeks", "For", "Geeks"]
 
# Sorting list of strings
words.sort()
 
print(words)


Output

[1, 2, 3, 4]
[1.68, 2.0, 2.01, 3.28, 3.67]
['For', 'Geeks', 'Geeks']

Sorting List in Descending Order

To sort a list in descending order, set the reverse parameter to True of the sort() function in Python.

Python3




# List of Integers
numbers = [1, 3, 4, 2]
 
# Sorting list of Integers
numbers.sort(reverse=True)
 
print(numbers)
 
# List of Floating point numbers
decimalnumber = [2.01, 2.00, 3.67, 3.28, 1.68]
 
# Sorting list of Floating point numbers
decimalnumber.sort(reverse=True)
 
print(decimalnumber)
 
# List of strings
words = ["Geeks", "For", "Geeks"]
 
# Sorting list of strings
words.sort(reverse=True)
 
print(words)


Output

[4, 3, 2, 1]
[3.67, 3.28, 2.01, 2.0, 1.68]
['Geeks', 'Geeks', 'For']

Sort with Custom Function using Key

To sort the list according to the user’s choice, pass a function to the key parameter of the Python sort() function.

Python3




def sortSecond(val):
    return val[1]
 
# list1 to demonstrate the use of sorting
# using second key
list1 = [(1,2),(3,3),(1,1)]
 
# sorts the array in ascending according to
# second element
list1.sort(key=sortSecond)
print(list1)
 
# sorts the array in descending according to
# second element
list1.sort(key=sortSecond,reverse=True)
print(list1)


Output

[(1, 1), (1, 2), (3, 3)]
[(3, 3), (1, 2), (1, 1)]

Difference between sorted() and sort() function in Python

Let us see the difference between the sorted() and sort() function in Python:

Python sorted()

Python sort()

The sorted() function returns a sorted list of the specific iterable object. The sort() method sorts the list.
We can specify ascending or descending order while using the sorted() function It sorts the list in ascending order by default.
Syntax: sorted(iterable, key=key, reverse=reverse) Syntax: list.sort(reverse=True|False, key=myFunc)
Its return type is a sorted list. We can also use it for sorting a list in descending order.
It can only sort a list that contains only one type of value. It sorts the list in place.

To know more please refer Python difference between the sorted() and sort() function.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments