Python Reference Library →

Python sorted() Function – Sort Any Iterable Easily

Let’s look at sorted() function – sort any iterable easily

  1. Notice

    sorted() creates a new sorted list from an iterable. The source object remains unchanged.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
items = [3, 2, 5]
ordered = sorted(items)
print(ordered)
print(items)
Live state

Top-level scope

items→Object 1
ordered→Object 2
Object 1 · list[3, 2, 5]Object 2 · list[2, 3, 5]
This step

sorted() creates a new sorted list from an iterable. The source object remains unchanged.

Picture sorted() function – sort any iterable easily

sorted() creates a new sorted list from an iterable. The source object remains unchanged.

Image brief

Alt text
A diagram for Python sorted() Function – Sort Any Iterable Easily. Instructions: items = [3, 2, 5] ordered = sorted(items) print(ordered) print(items). Displayed result: [2, 3, 5] [3, 2, 5]. The final example bindings are items --> object 1 (list); ordered --> object 2 (list). object 1: [3, 2, 5]; object 2: [2, 3, 5]
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python sorted() Function – Sort Any Iterable Easily". Teaching focus: sorted() creates a new sorted list from an iterable. The source object remains unchanged. Depict exactly the relationships and labels in this reference diagram: 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] Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python sorted() Function – Sort Any Iterable Easily. Instructions: items = [3, 2, 5] ordered = sorted(items) print(ordered) print(items). Displayed result: [2, 3, 5] [3, 2, 5]. The final example bindings are items --> object 1 (list); ordered --> object 2 (list). object 1: [3, 2, 5]; object 2: [2, 3, 5]

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
items = [3, 2, 5]
ordered = sorted(items)
print(ordered)
print(items)
Live state

Top-level scope

items→Object 1
ordered→Object 2
Object 1 · list[3, 2, 5]Object 2 · list[2, 3, 5]
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
[2, 3, 5]
[3, 2, 5]

What do you expect this program to print?

What does this example display?

items = [3, 2, 5]
ordered = sorted(items)
print(ordered)
print(items)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

[2, 3, 5] [3, 2, 5]

Follow the printed values in order. The output is: [2, 3, 5] [3, 2, 5]

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

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.

More examples and reference

Python sorted() Function

The 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.

Syntax

sorted(iterable, *, key=None, reverse=False)

Parameters:

  • iterable – The sequence to be sorted (like list, tuple, set, dictionary keys, string, etc.)
  • key (optional) – A function to customize sorting logic
  • reverse (optional) – If True, sorts in descending order

Returns:

  • A new sorted list.

Example 1: Sorting a List

numbers = [4, 2, 9, 1]
print(sorted(numbers))
[1, 2, 4, 9]

Example 2: Sorting in Reverse Order

numbers = [4, 2, 9, 1]
print(sorted(numbers, reverse=True))
[9, 4, 2, 1]

Example 3: Sorting Strings Alphabetically

fruits = ["banana", "apple", "cherry"]
print(sorted(fruits))
['apple', 'banana', 'cherry']

Example 4: Sorting with a Key Function

# Sort by length of strings
words = ["apple", "banana", "fig", "date"]
print(sorted(words, key=len))
['fig', 'date', 'apple', 'banana']

Example 5: Sorting Tuples by Second Element

pairs = [(1, 3), (2, 2), (4, 1)]
print(sorted(pairs, key=lambda x: x[1]))
[(4, 1), (2, 2), (1, 3)]

Use Cases

  • Alphabetical sorting of names or words
  • Sorting numbers for comparisons
  • Custom sorting using key, like sorting objects by attributes
  • Sorting dictionary keys or values

Common Mistakes

  • Expecting sorted() to change the original list – it returns a new one
  • For descending order, forgetting to use reverse=True
  • Passing non-iterable values like integers

Interview Tip

Use sorted() with custom key functions to solve problems like sorting students by grades, or sorting tuples by second item.

Summary

  • sorted() returns a new sorted list from any iterable
  • Supports key for custom sort logic
  • Set reverse=True for descending order

Practice Problem

Write 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)

What to remember

Connect what you noticed in the example with the idea below.

Remember

sorted() creates a new sorted list from an iterable. The source object remains unchanged.

Watch out for this idea

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.

    Reference library →