0
0
Pythonprogramming~10 mins

sorted() function in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - sorted() function
Start with iterable
Call sorted()
Compare elements
Arrange elements in order
Return new sorted list
End
The sorted() function takes an iterable, compares its elements, arranges them in order, and returns a new sorted list.
Execution Sample
Python
numbers = [3, 1, 4, 1]
sorted_numbers = sorted(numbers)
print(sorted_numbers)
This code sorts the list 'numbers' and prints the sorted list.
Execution Table
StepActionIterable StateComparisonResult
1Start with numbers = [3, 1, 4, 1][3, 1, 4, 1]N/AN/A
2Call sorted(numbers)[3, 1, 4, 1]Compare 3 and 11 < 3, so 1 comes first
3Compare 3 and 4[3, 1, 4, 1]3 < 43 comes before 4
4Compare 1 and 3[3, 1, 4, 1]1 < 31 comes before 3
5Arrange elements[3, 1, 4, 1]N/A[1, 1, 3, 4]
6Return new sorted list[3, 1, 4, 1]N/A[1, 1, 3, 4]
7Print sorted_numbers[3, 1, 4, 1]N/AOutput: [1, 1, 3, 4]
💡 All elements compared and arranged; sorted() returns new sorted list.
Variable Tracker
VariableStartAfter sorted()Final
numbers[3, 1, 4, 1][3, 1, 4, 1][3, 1, 4, 1]
sorted_numbersN/A[1, 1, 3, 4][1, 1, 3, 4]
Key Moments - 3 Insights
Does sorted() change the original list?
No, sorted() returns a new sorted list and does not modify the original list 'numbers' as shown in variable_tracker rows 1 and 2.
What type of object does sorted() return?
sorted() always returns a new list, even if the input is another iterable type, as shown in execution_table step 6.
Why do we see multiple comparisons in the execution_table?
sorted() compares elements pairwise to decide their order, which is why steps 2 to 4 show comparisons before arranging the final list.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is the arranged list?
A[4, 3, 1, 1]
B[1, 1, 3, 4]
C[3, 1, 4, 1]
D[1, 3, 1, 4]
💡 Hint
Check the 'Result' column at step 5 in the execution_table.
According to variable_tracker, what is the value of 'numbers' after sorted() is called?
A[3, 1, 4, 1]
B[1, 1, 3, 4]
CN/A
D[]
💡 Hint
Look at the 'numbers' row in variable_tracker after sorted() call.
If we change 'numbers' to a tuple, what will sorted() return?
AA sorted tuple
BAn error
CA sorted list
DThe original tuple unchanged
💡 Hint
Recall that sorted() always returns a list regardless of input type.
Concept Snapshot
sorted(iterable) returns a new list with elements sorted in ascending order.
It does not change the original iterable.
Works with any iterable (list, tuple, string).
You can provide key and reverse parameters for custom sorting.
Always returns a list.
Full Transcript
The sorted() function takes an iterable like a list and returns a new list with the elements arranged in ascending order. It compares elements pairwise to decide their order. The original list remains unchanged. The returned object is always a list, even if the input is a tuple or string. This example shows sorting a list of numbers and printing the sorted result.