Sort a List in Python

In Python, you can sort a list using the sort() method (which modifies the list in place) or the sorted() function (which returns a new sorted list). Sorting can be done in ascending or descending order and can also be customized with a sorting key.


Examples

1. Sorting a List in Ascending Order

We can use the sort() method to sort a list of numbers in ascending order.

</>
Copy
# Creating a list of numbers
numbers = [5, 2, 9, 1, 5, 6]

# Sorting the list in ascending order
numbers.sort()

# Printing the sorted list
print("Sorted List:", numbers)

In this example, we have a list named numbers containing unsorted integers. The sort() method is called on the list, which modifies it in place to arrange the elements in ascending order.

Output:

Sorted List: [1, 2, 5, 5, 6, 9]

2. Sorting a List in Descending Order

To sort a list in descending order, we use the sort() method with the reverse=True argument.

</>
Copy
# Creating a list of numbers
numbers = [3, 8, 1, 6, 0, 9]

# Sorting the list in descending order
numbers.sort(reverse=True)

# Printing the sorted list
print("Sorted List in Descending Order:", numbers)

Here, the sort() method is used again, but with the reverse=True parameter. This modifies the numbers list in place to arrange the elements in descending order.

Output:

Sorted List in Descending Order: [9, 8, 6, 3, 1, 0]

3. Sorting a List Using the sorted() Function

The sorted() function returns a new sorted list instead of modifying the original list.

</>
Copy
# Creating a list of numbers
numbers = [4, 7, 2, 9, 0]

# Sorting using sorted() function
sorted_numbers = sorted(numbers)

# Printing the original and sorted lists
print("Original List:", numbers)
print("Sorted List:", sorted_numbers)

The sorted() function takes numbers as input and returns a new sorted list, stored in sorted_numbers. The original list remains unchanged.

Output:

Original List: [4, 7, 2, 9, 0]
Sorted List: [0, 2, 4, 7, 9]

4. Sorting a List of Strings Alphabetically

We can also sort a list of strings alphabetically using sort().

</>
Copy
# Creating a list of words
words = ["banana", "apple", "cherry", "blueberry"]

# Sorting the list alphabetically
words.sort()

# Printing the sorted list
print("Sorted Words:", words)

The sort() method arranges the list words in alphabetical order, modifying it in place.

Output:

Sorted Words: ['apple', 'banana', 'blueberry', 'cherry']

5. Sorting with a Custom Key (Sorting by Length)

We can use the key parameter to sort elements based on specific criteria. Here, we sort a list of words by their length.

</>
Copy
# Creating a list of words
words = ["elephant", "cat", "hippopotamus", "dog"]

# Sorting words by length
words.sort(key=len)

# Printing the sorted list
print("Sorted Words by Length:", words)

The key=len argument tells the sort() method to sort based on the length of each string in the words list.

Output:

Sorted Words by Length: ['cat', 'dog', 'elephant', 'hippopotamus']

Conclusion

  1. sort(): Sorts the list in place (modifies original list).
  2. sorted(): Returns a new sorted list without modifying the original.
  3. Sorting in descending order: Use reverse=True with sort() or sorted().
  4. Sorting with a custom key: Use the key parameter to define sorting rules.

Choosing between sort() and sorted() depends on whether you want to modify the original list or not.