×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python - Sort Lists

Last Updated : May 01, 2025

In Python, sorting lists is simple and can be done using the built-in methods like sort() and sorted(). These methods allow you to arrange list elements in ascending or descending order. In this chapter, we will learn sorting lists using different approaches with the help of examples.

Sorting a List in Ascending Order Using sort()

The sort() method sorts the list in-place, meaning it changes the original list to be sorted in ascending order. By default, it sorts in increasing order.

Example

In this example, we are sorting a list of cities in alphabetical (ascending) order using the sort() method.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Berlin", "Tokyo"]

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

# Displaying the sorted list
print(cities)

The output of the above code would be:

['Berlin', 'London', 'New York', 'Paris', 'Tokyo']

Sorting a List in Descending Order Using sort()

You can sort a list in descending order by passing the argument reverse=True to the sort() method. This sorts the list from highest to lowest (reverse alphabetical order for strings).

Example

In this example, we are sorting a list of cities in reverse (descending) order using the sort() method with the reverse=True argument.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Berlin", "Tokyo"]

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

# Displaying the sorted list
print(cities)

The output of the above code would be:

['Tokyo', 'Paris', 'New York', 'London', 'Berlin']

Sorting a List Without Modifying the Original List Using sorted()

The sorted() function returns a new sorted list without modifying the original list. It works similarly to the sort() method but creates a copy of the list.

Example

In this example, we are using sorted() to return a new sorted list without altering the original list.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Berlin", "Tokyo"]

# Getting a sorted list without modifying the original list
sorted_cities = sorted(cities)

# Displaying the original and sorted lists
print("Original list:", cities)
print("Sorted list:", sorted_cities)

The output of the above code would be:

Original list: ['New York', 'London', 'Paris', 'Berlin', 'Tokyo']
Sorted list: ['Berlin', 'London', 'New York', 'Paris', 'Tokyo']

Sorting a List of Numbers in Ascending Order

The sort() and sorted() methods can also be used to sort lists of numbers in ascending order. This works in the same way as sorting strings.

Example

In this example, we are sorting a list of numbers in ascending order using the sort() method.

# Creating a list of numbers
numbers = [42, 17, 99, 56, 13]

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

# Displaying the sorted list
print(numbers)

The output of the above code would be:

[13, 17, 42, 56, 99]

Sorting a List of Numbers in Descending Order

As with strings, you can sort a list of numbers in descending order by passing the argument reverse=True to either the sort() or sorted() method.

Example

In this example, we are sorting a list of numbers in descending order using the sort() method with the reverse=True argument.

# Creating a list of numbers
numbers = [42, 17, 99, 56, 13]

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

# Displaying the sorted list
print(numbers)

The output of the above code would be:

[99, 56, 42, 17, 13]

Exercise

Select the correct option to complete each statement about sorting lists in Python.

  1. Which method is used to sort a list in Python?
  2. What is the default sorting order of the sort() method in Python?
  3. Which function can be used to return a sorted version of the list without modifying the original list?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.