Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Sorting Lists in Python

Learn how to sort lists in Python with this step-by-step guide. We’ll cover the basics of sorting, different methods for sorting lists, and provide code examples to help you understand each concept. …


Updated June 21, 2023

Learn how to sort lists in Python with this step-by-step guide. We’ll cover the basics of sorting, different methods for sorting lists, and provide code examples to help you understand each concept.

Definition

Sorting a list in Python means arranging its elements in a specific order, either ascending or descending. This is useful when working with large datasets, as it allows you to quickly identify patterns or outliers. In this article, we’ll explore the different methods for sorting lists in Python.

Step-by-Step Explanation

Method 1: Built-in sort() Function

The most straightforward way to sort a list in Python is by using its built-in sort() function. Here’s an example:

# Create a sample list of integers
numbers = [64, 34, 25, 12, 22, 11, 90]

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

print(numbers)  # Output: [11, 12, 22, 25, 34, 64, 90]

In this example, we create a sample list of integers and then call the sort() function to arrange them in ascending order. Note that the sort() function sorts the list in-place, meaning it modifies the original list.

Method 2: Built-in sorted() Function

While the sort() function is convenient for small lists, it’s often more useful to create a new sorted list without modifying the original one. In this case, we can use the built-in sorted() function:

# Create a sample list of integers
numbers = [64, 34, 25, 12, 22, 11, 90]

# Sort the list in ascending order and return a new sorted list
sorted_numbers = sorted(numbers)

print(sorted_numbers)  # Output: [11, 12, 22, 25, 34, 64, 90]

As you can see, the sorted() function creates a new sorted list without modifying the original one.

Method 3: Sorting using Lambda Function and sort()

If you need to sort a list based on multiple criteria or use custom comparison functions, you can use lambda functions with the sort() method:

# Create a sample list of tuples containing names and ages
people = [
    ('John', 25),
    ('Alice', 30),
    ('Bob', 35)
]

# Sort the list in ascending order based on age using a lambda function
people.sort(key=lambda x: x[1])

print(people)  # Output: [('John', 25), ('Alice', 30), ('Bob', 35)]

In this example, we use a lambda function to specify that we want to sort the list based on the second element of each tuple (the age).

Additional Methods

There are additional methods for sorting lists in Python, including:

  • The heapq module’s nsmallest() and nlargest() functions for selecting the smallest or largest elements.
  • The itertools module’s chain() function for sorting multiple iterables together.

However, these advanced techniques are outside the scope of this introductory guide.

Conclusion

Sorting lists in Python is an essential skill to master when working with data structures. With the built-in sort() and sorted() functions, you can easily sort lists of various types. Additionally, using lambda functions with the sort() method allows for custom sorting criteria. Whether you’re a beginner or experienced developer, mastering these techniques will make your work more efficient and enjoyable.

Code Snippets

Here are some code snippets to demonstrate each concept:

  • Method 1: Built-in sort() function

numbers = [64, 34, 25, 12, 22, 11, 90] numbers.sort() print(numbers)


*   **Method 2: Built-in sorted() function**
    ```python
numbers = [64, 34, 25, 12, 22, 11, 90]
sorted_numbers = sorted(numbers)
print(sorted_numbers)
  • Method 3: Sorting using lambda function and sort()

people = [ (‘John’, 25), (‘Alice’, 30), (‘Bob’, 35) ] people.sort(key=lambda x: x[1]) print(people)

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp