Bird
Raised Fist0
Pythonprogramming~10 mins

Sorting and reversing lists in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Sorting and reversing lists
Start with list
Choose operation
Sort list
Sorted list
End
We start with a list, then choose to either sort it or reverse it, resulting in a new list order.
Execution Sample
Python
numbers = [4, 2, 9, 1]
numbers.sort()
print(numbers)
numbers.reverse()
print(numbers)
This code sorts the list in ascending order, prints it, then reverses the list and prints it again.
Execution Table
StepOperationList BeforeActionList AfterOutput
1Start[4, 2, 9, 1]None[4, 2, 9, 1]
2sort()[4, 2, 9, 1]Sort ascending[1, 2, 4, 9]
3print()[1, 2, 4, 9]Print list[1, 2, 4, 9][1, 2, 4, 9]
4reverse()[1, 2, 4, 9]Reverse order[9, 4, 2, 1]
5print()[9, 4, 2, 1]Print list[9, 4, 2, 1][9, 4, 2, 1]
6End[9, 4, 2, 1]None[9, 4, 2, 1]
💡 All operations done, list sorted then reversed and printed twice.
Variable Tracker
VariableStartAfter sort()After reverse()Final
numbers[4, 2, 9, 1][1, 2, 4, 9][9, 4, 2, 1][9, 4, 2, 1]
Key Moments - 3 Insights
Why does the list change after calling sort()?
Because sort() changes the list in place, it rearranges the original list instead of creating a new one. See execution_table step 2 where the list changes from [4, 2, 9, 1] to [1, 2, 4, 9].
Does reverse() sort the list?
No, reverse() only flips the order of elements. It does not sort. Look at step 4 in execution_table: the list [1, 2, 4, 9] becomes [9, 4, 2, 1], which is reversed but not sorted.
What if I want a sorted list but keep the original unchanged?
Use the sorted() function which returns a new sorted list without changing the original. This example uses sort() which changes the list itself, as shown in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the list output?
A[4, 2, 9, 1]
B[1, 2, 4, 9]
C[9, 4, 2, 1]
D[1, 9, 2, 4]
💡 Hint
Check the Output column at step 3 in execution_table.
At which step does the list get reversed?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the Operation and Action columns in execution_table to find when reverse() is called.
If we replace numbers.sort() with sorted(numbers), what changes in variable_tracker?
Anumbers list stays the same after sorting
Bnumbers list changes after sorting
Cnumbers list becomes empty
Dnumbers list reverses automatically
💡 Hint
Recall that sorted() returns a new list and does not modify the original list, unlike sort().
Concept Snapshot
Sorting and reversing lists in Python:
- Use list.sort() to sort the list in place (changes original).
- Use list.reverse() to reverse the list order in place.
- Use sorted(list) to get a new sorted list without changing original.
- Both sort() and reverse() do not return a new list.
- Print to see the current list state after operations.
Full Transcript
We start with a list of numbers. First, we call sort() which rearranges the list in ascending order. Then we print the sorted list. Next, we call reverse() which flips the order of the list elements. We print the reversed list. The variable 'numbers' changes after each operation because sort() and reverse() modify the list in place. If you want to keep the original list unchanged, use the sorted() function instead of sort().

Practice

(1/5)
1. What does the list.sort() method do to a list in Python?
easy
A. It arranges the list items in ascending order.
B. It reverses the order of the list items.
C. It removes duplicate items from the list.
D. It creates a new sorted list without changing the original.

Solution

  1. Step 1: Understand the purpose of list.sort()

    The list.sort() method changes the original list to arrange its items in ascending order.
  2. Step 2: Differentiate from other list methods

    Unlike list.reverse(), which flips the list order, list.sort() orders items from smallest to largest.
  3. Final Answer:

    It arranges the list items in ascending order. -> Option A
  4. Quick Check:

    list.sort() = ascending order [OK]
Hint: Remember: sort() orders ascending by default [OK]
Common Mistakes:
  • Confusing sort() with reverse()
  • Thinking sort() returns a new list
  • Assuming sort() removes duplicates
2. Which of the following is the correct syntax to sort a list named numbers in descending order?
easy
A. numbers.sort(reverse=True)
B. numbers.sort(reverse=1)
C. numbers.sort(descending=True)
D. numbers.reverse(sort=True)

Solution

  1. Step 1: Identify correct parameter for descending sort

    The sort() method accepts reverse=True to sort in descending order.
  2. 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.
  3. Final Answer:

    numbers.sort(reverse=True) -> Option A
  4. Quick Check:

    sort(reverse=True) = descending order [OK]
Hint: Use reverse=True inside sort() for descending order [OK]
Common Mistakes:
  • Using reverse=1 instead of reverse=True
  • Using non-existent parameters like descending=True
  • Calling reverse() with parameters
3. What is the output of the following code?
items = [3, 1, 4, 2]
items.sort()
items.reverse()
print(items)
medium
A. [1, 2, 3, 4]
B. [3, 1, 4, 2]
C. [4, 3, 2, 1]
D. [2, 3, 1, 4]

Solution

  1. Step 1: Apply items.sort()

    This sorts the list in ascending order: [1, 2, 3, 4].
  2. Step 2: Apply items.reverse()

    This reverses the sorted list, resulting in [4, 3, 2, 1].
  3. Final Answer:

    [4, 3, 2, 1] -> Option C
  4. Quick Check:

    sort() then reverse() = descending list [OK]
Hint: sort() then reverse() = descending order [OK]
Common Mistakes:
  • Thinking reverse() sorts the list
  • Assuming print shows original list
  • Confusing order of method calls
4. The following code is intended to sort the list data in descending order. What is wrong?
data = [5, 2, 9, 1]
data.reverse()
data.sort()
print(data)
medium
A. The code will cause a syntax error.
B. The reverse() call should come after sort().
C. The sort() method does not exist for lists.
D. The list is already sorted, so no change happens.

Solution

  1. Step 1: Analyze method order

    The code reverses the list first, then sorts it ascending, which cancels the reverse effect.
  2. Step 2: Correct method order for descending sort

    To get descending order, first sort ascending, then reverse the list.
  3. Final Answer:

    The reverse() call should come after sort(). -> Option B
  4. Quick Check:

    sort() then reverse() = descending order [OK]
Hint: Sort first, then reverse for descending order [OK]
Common Mistakes:
  • Reversing before sorting cancels sorting effect
  • Thinking reverse() sorts the list
  • Assuming method order does not matter
5. You have a list of words: words = ['apple', 'banana', 'cherry', 'date']. You want to sort them in reverse alphabetical order without changing the original list. Which code snippet achieves this?
hard
A. sorted_words = words.sort(reverse=True)
B. words.sort(reverse=True)
C. words.reverse(); words.sort()
D. sorted_words = sorted(words, reverse=True)

Solution

  1. Step 1: Understand difference between sort() and sorted()

    sort() changes the original list; sorted() returns a new sorted list.
  2. Step 2: Choose method that sorts in reverse without changing original

    Using sorted(words, reverse=True) returns a new list sorted in reverse alphabetical order, leaving words unchanged.
  3. Final Answer:

    sorted_words = sorted(words, reverse=True) -> Option D
  4. Quick Check:

    sorted() returns new sorted list [OK]
Hint: Use sorted() to keep original list unchanged [OK]
Common Mistakes:
  • Using sort() which changes original list
  • Chaining reverse() before sort() incorrectly
  • Assigning sort() result which is None