Lists — Sorting & Searching
Sort and search through list data. Read the lesson first, then move through the exercises in order.
After reading
Practice Arena
Begin with the first exercise, then continue step by step through the module.
Start with Sort AlphabeticallyStudy 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".
pythonscores = [45, 99, 21, 88] scores.sort() print(scores) # Output: [21, 45, 88, 99]
It works on strings, too. Python will sort them alphabetically:
pythonnames = ["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.
pythonscores = [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.
pythonoriginal = [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).
pythonvalid_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().
pythonletters = ["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
inkeyword - 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.
Sort Alphabetically
Complete the "Sort Alphabetically" exercise.
Sort Descending
Complete the "Sort Descending" exercise.
Check if Sorted
Complete the "Check if Sorted" exercise.
Find Index
Complete the "Find Index" exercise.
Calculate Median
Complete the "Calculate Median" exercise.