0
0
Pythonprogramming~10 mins

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

Choose your learning style9 modes available
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().