Remember
sorted() creates a new sorted list from an iterable. The source object remains unchanged.
sorted() creates a new sorted list from an iterable. The source object remains unchanged.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
items = [3, 2, 5]
ordered = sorted(items)
print(ordered)
print(items)Top-level scope
[3, 2, 5]Object 2 · list[2, 3, 5]sorted() creates a new sorted list from an iterable. The source object remains unchanged.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
items = [3, 2, 5]
ordered = sorted(items)
print(ordered)
print(items)
|
v
DISPLAYED RESULT
[2, 3, 5]
[3, 2, 5]
FINAL BINDINGS
items --> object 1 (list)
ordered --> object 2 (list)
object 1: [3, 2, 5]
object 2: [2, 3, 5]Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
items = [3, 2, 5]
ordered = sorted(items)
print(ordered)
print(items)Top-level scope
[3, 2, 5]Object 2 · list[2, 3, 5]The example has finished. Compare the final output with the values and references above.
[2, 3, 5] [3, 2, 5]
What do you expect this program to print?
items = [3, 2, 5]
ordered = sorted(items)
print(ordered)
print(items)sorted() FunctionThe sorted() function in Python is used to sort elements of any iterable (like a list, tuple, or string) and return a new sorted list. It does not modify the original data.
sorted(iterable, *, key=None, reverse=False)
iterable – The sequence to be sorted (like list, tuple, set, dictionary keys, string, etc.)key (optional) – A function to customize sorting logicreverse (optional) – If True, sorts in descending ordernumbers = [4, 2, 9, 1]
print(sorted(numbers))
[1, 2, 4, 9]
numbers = [4, 2, 9, 1]
print(sorted(numbers, reverse=True))
[9, 4, 2, 1]
fruits = ["banana", "apple", "cherry"]
print(sorted(fruits))
['apple', 'banana', 'cherry']
# Sort by length of strings
words = ["apple", "banana", "fig", "date"]
print(sorted(words, key=len))
['fig', 'date', 'apple', 'banana']
pairs = [(1, 3), (2, 2), (4, 1)]
print(sorted(pairs, key=lambda x: x[1]))
[(4, 1), (2, 2), (1, 3)]
key, like sorting objects by attributessorted() to change the original list – it returns a new onereverse=TrueUse sorted() with custom key functions to solve problems like sorting students by grades, or sorting tuples by second item.
sorted() returns a new sorted list from any iterablekey for custom sort logicreverse=True for descending orderWrite a program that reads 5 space-separated numbers and prints them in descending order.
nums = list(map(int, input("Enter 5 numbers: ").split()))
sorted_nums = sorted(nums, reverse=True)
print("Sorted descending:", sorted_nums)
Connect what you noticed in the example with the idea below.
sorted() creates a new sorted list from an iterable. The source object remains unchanged.
sorted() always sorts a source list in place.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.