BeginnerLesson firstCategory 20 of 20

Lists — Sorting & Searching

Sort and search through list data. Read the lesson first, then move through the exercises in order.

6 Sections5 Exercises

After reading

Practice Arena

Begin with the first exercise, then continue step by step through the module.

Start with Sort Alphabetically

Study Material

Read the full lesson

Organizing chaos

Data is rarely perfectly organized when you first get it. Numbers are scrambled, names are out of alphabetical order.

Python gives lists built-in superpowers to sort their own data efficiently.

In-place sorting with .sort()

Every list has a .sort() method. When you call it, the list rearranges its own elements permanently. This is called sorting "in-place".

python
scores = [45, 99, 21, 88] scores.sort() print(scores) # Output: [21, 45, 88, 99]

It works on strings, too. Python will sort them alphabetically:

python
names = ["Zack", "Alice", "Charlie"] names.sort() print(names) # Output: ['Alice', 'Charlie', 'Zack']

Reversing the sort

If you want the highest scores first, or reverse-alphabetical order, simply pass reverse=True inside the parentheses.

python
scores = [45, 99, 21, 88] scores.sort(reverse=True) print(scores) # Output: [99, 88, 45, 21]

Generating a new sorted list with sorted()

Sometimes you want to display the data sorted, but you don't want to destroy the original order of your list.

Instead of the .sort() method, you can use the built-in sorted() function. It takes your list, makes a copy, sorts the copy, and hands it back to you. The original list is completely unharmed.

python
original = [3, 1, 2] clean_copy = sorted(original) print(f"Original: {original}") print(f"Sorted Copy: {clean_copy}")

Finding things with in

Before you try to remove an item or process it, it is often wise to check if it actually exists in the list first.

Python makes this breathtakingly easy with the in keyword. It returns a simple Boolean (True or False).

python
valid_colors = ["red", "green", "blue"] print("red" in valid_colors) # Output: True print("yellow" in valid_colors) # Output: False if "green" in valid_colors: print("Green is a valid choice!")

Finding the index with .index()

If you know the item exists, but you need to know where it is, use .index().

python
letters = ["A", "B", "C", "D"] position = letters.index("C") print(f"'C' is located at index {position}") # Output: 'C' is located at index 2

Warning: If the item isn't in the list, .index() will crash with a ValueError. Use the in keyword first to be safe!

What this lesson should give you

After this lesson, you should understand how to:

  • permanently sort a list in ascending or descending order using .sort()
  • sort a copy of a list without mutating the original using sorted()
  • elegantly check if an item exists inside a list using the in keyword
  • find the exact index position of an item using .index()

Interactive

Exercises for this topic

These exercises follow the exact order of the lesson. Move step-by-step from reading into coding.