sort() in Python (original) (raw)

Last Updated : 05 May, 2025

**sort() method in Python sort the elements of a list in ascending or descending order. It modifies the original list in place, meaning it does not return a new list, but instead changes the list it is called on. **Example:

Python `

a = [5, 3, 8, 1, 2] a.sort() print(a)

a.sort(reverse=True) print(a)

`

Output

[1, 2, 3, 5, 8] [8, 5, 3, 2, 1]

**Explanation:

Syntax of sort()

list.sort(reverse=False, key=None, m=None)

**Parameters:

**Returns: This method returns None as it does not create a new list, it simply modifies the original list in place.

Examples of sort()

**Example 1: In this example, we arrange a list of strings in alphabetical (lexicographical) order.

Python `

a = ["banana", "apple", "cherry"] a.sort() print(a)

`

Output

['apple', 'banana', 'cherry']

**Explanation: a.sort(reverse=True) sorts the list in descending order, from Z to A, based on their Unicode code points.

**Example 2: In this example, we sort a list of strings by their length using the key parameter of the **sort() method. By passing the built-in len function, the strings are sorted in ascending order based on length.

Python `

a = ["sun", "moonlight", "sky"] a.sort(key=len) print(a)

`

Output

['sun', 'sky', 'moonlight']

**Explanation: a.sort(key=len) sorts the list based on the length of each string, arranging them in ascending order of their lengths.

**Example 3: In this example, we sort a list of dictionaries by the value of the “age” key using the key parameter of the **sort() method.

Python `

d = [ {"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}, {"name": "Charlie", "age": 35} ]

d.sort(key=lambda person: person["age"]) print(d)

`

Output

[{'name': 'Bob', 'age': 25}, {'name': 'Alice', 'age': 30}, {'name': 'Charlie', 'age': 35}]

**Explanation: d.sort(key=lambda person: person[“age”]) sorts the list of dictionaries by the “age” key in ascending order. The lambda function extracts the age from each dictionary for comparison.

**Example 4: In this example, we sort a list of tuples by the second element (age) using the key parameter with a lambda function, sorting the list in ascending order by age.

Python `

a = [("Alice", 25), ("Bob", 30), ("Charlie", 22)] a.sort(key=lambda x: x[1]) print(a)

`

Output

[('Charlie', 22), ('Alice', 25), ('Bob', 30)]

**Explanation: a.sort(key=lambda x: x[1]) sorts the list of tuples by the second element **age in ascending order. The lambda function accesses the second element of each tuple for comparison.

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

Understanding the difference between sorted() and sort() helps you choose the right tool for your needs. Both sort elements but differ in memory usage, stability, and compatibility. Let’s break it down in the following table.

Feature sorted() sort()
Return Type Returns a new sorted list Sorts the list in place
Order Can specify ascending/descending Default is ascending
Applicable to Any iterable (not just lists) Only lists
Stability Stable (maintains relative order) May not be stable
Memory Usage Requires extra memory Sorts in place, no extra memory
Key Parameter Supports key for custom sorting Supports key for custom sorting
Time Complexity O(n log n) O(n log n)