Sorting helps organize items in order, like arranging books by title. Reversing flips the order, like turning a stack upside down.
Sorting and reversing lists in Python
Start learning this pattern below
Jump into concepts and practice - no test required
my_list = [3, 1, 4, 2] # Sort the list in ascending order my_list.sort() # Sort the list in descending order my_list.sort(reverse=True) # Reverse the list order (no sorting) my_list.reverse()
sort() changes the list itself.
reverse() flips the list order without sorting.
numbers = []
numbers.sort()
print(numbers)letters = ['a'] letters.sort() print(letters)
fruits = ['banana', 'apple', 'cherry'] fruits.sort() print(fruits)
fruits = ['banana', 'apple', 'cherry'] fruits.sort(reverse=True) print(fruits)
numbers = [1, 2, 3] numbers.reverse() print(numbers)
This program shows how to sort a list in ascending and descending order, then reverse it. It prints the list at each step so you can see the changes.
def print_list_state(label, items): print(f"{label}: {items}") numbers = [5, 2, 9, 1, 5, 6] print_list_state("Original list", numbers) # Sort ascending numbers.sort() print_list_state("Sorted ascending", numbers) # Sort descending numbers.sort(reverse=True) print_list_state("Sorted descending", numbers) # Reverse the list numbers.reverse() print_list_state("Reversed list", numbers)
Sorting with sort() changes the original list and runs in O(n log n) time.
Reversing with reverse() runs in O(n) time and just flips the list order.
Common mistake: Using sorted() returns a new sorted list but does not change the original list.
Use sort() when you want to change the list order permanently. Use reverse() when you want to flip the current order.
Use list.sort() to arrange items in order.
Use list.sort(reverse=True) to sort in reverse order.
Use list.reverse() to flip the list order without sorting.
Practice
list.sort() method do to a list in Python?Solution
Step 1: Understand the purpose of
Thelist.sort()list.sort()method changes the original list to arrange its items in ascending order.Step 2: Differentiate from other list methods
Unlikelist.reverse(), which flips the list order,list.sort()orders items from smallest to largest.Final Answer:
It arranges the list items in ascending order. -> Option AQuick Check:
list.sort() = ascending order [OK]
- Confusing sort() with reverse()
- Thinking sort() returns a new list
- Assuming sort() removes duplicates
numbers in descending order?Solution
Step 1: Identify correct parameter for descending sort
Thesort()method acceptsreverse=Trueto sort in descending order.Step 2: Check syntax correctness
numbers.sort(reverse=True) uses the correct syntax:numbers.sort(reverse=True). Other options use invalid parameters or method calls.Final Answer:
numbers.sort(reverse=True) -> Option AQuick Check:
sort(reverse=True) = descending order [OK]
- Using reverse=1 instead of reverse=True
- Using non-existent parameters like descending=True
- Calling reverse() with parameters
items = [3, 1, 4, 2] items.sort() items.reverse() print(items)
Solution
Step 1: Apply
This sorts the list in ascending order: [1, 2, 3, 4].items.sort()Step 2: Apply
This reverses the sorted list, resulting in [4, 3, 2, 1].items.reverse()Final Answer:
[4, 3, 2, 1] -> Option CQuick Check:
sort() then reverse() = descending list [OK]
- Thinking reverse() sorts the list
- Assuming print shows original list
- Confusing order of method calls
data in descending order. What is wrong?data = [5, 2, 9, 1] data.reverse() data.sort() print(data)
Solution
Step 1: Analyze method order
The code reverses the list first, then sorts it ascending, which cancels the reverse effect.Step 2: Correct method order for descending sort
To get descending order, first sort ascending, then reverse the list.Final Answer:
The reverse() call should come after sort(). -> Option BQuick Check:
sort() then reverse() = descending order [OK]
- Reversing before sorting cancels sorting effect
- Thinking reverse() sorts the list
- Assuming method order does not matter
words = ['apple', 'banana', 'cherry', 'date']. You want to sort them in reverse alphabetical order without changing the original list. Which code snippet achieves this?Solution
Step 1: Understand difference between
sort()andsorted()sort()changes the original list;sorted()returns a new sorted list.Step 2: Choose method that sorts in reverse without changing original
Usingsorted(words, reverse=True)returns a new list sorted in reverse alphabetical order, leavingwordsunchanged.Final Answer:
sorted_words = sorted(words, reverse=True) -> Option DQuick Check:
sorted() returns new sorted list [OK]
- Using sort() which changes original list
- Chaining reverse() before sort() incorrectly
- Assigning sort() result which is None
