Concepts / Understanding Iterators and Generators

Understanding Iterators and Generators

The word iterator matters for reversed().

  • Programming

The Tuple Question

Lists and tuples are both sequences that can hold multiple items, so it is natural to expect them to offer similar operations. A list has sort() and reverse() methods. A tuple does not. The reason is not that tuples cannot be ordered or traversed backward. The reason is that those methods modify a sequence in place, while tuples are immutable. Python instead provides the built-in functions sorted() and reversed(), which work without changing the original sequence.

Two Ways to Transform a Sequence

An in-place method operates on the existing mutable sequence. For a list, sort() changes the list's order, and reverse() changes the list's order to the opposite direction. These operations are appropriate when the old order is no longer needed. A built-in function takes a sequence as input and produces a separate result. sorted() produces a new list in ascending order, while reversed() produces an iterator that yields the elements in reverse order. In both cases, the original sequence is not modified.

Listoriginal orderListsorted orderListoriginal orderNew listsorted ordersort() changessorted() produces
What changes when a list method changes the existing list compared with when sorted() or reversed() produces a separate result?
OperationWorks withResult or effectOriginal sequence
sort()ListsSorts the existing list in placeChanged
reverse()ListsReverses the existing list in placeChanged
sorted()Sequences such as lists, tuples, and stringsReturns a new list in ascending orderUnchanged
reversed()Sequences such as lists, tuples, and stringsReturns an iterator yielding elements in reverse orderUnchanged

The operation determines whether the original sequence is changed and what kind of result is produced.

Why Tuples Have No In-Place Methods

Definition

A tuple is immutable, meaning that its contents cannot be modified in place. The sort() and reverse() methods modify a sequence in place, so providing those methods for tuples would conflict with tuple immutability. Lists are mutable, so they can provide those methods.

Listmutablesort()in-place changesorted()new listreverse()in-place changeTupleimmutablereversed()iteratorsupportssupportscan usecan use
Why can a list provide methods that change its order while a tuple cannot provide equivalent in-place methods?
In the real world

Suppose a tuple contains a fixed collection of values whose original arrangement must remain available. Calling sorted() on that tuple can produce a new list in ascending order, while the tuple remains unchanged. This gives you an ordered working result without asking the tuple itself to change.

Following sorted() and reversed()

Worked example

Sorting and Reversing a Tuple

A tuple contains the values 4, 1, and 3. You need an ascending result and a reverse-order traversal, but the tuple must remain unchanged.

  1. 1
    Sort the tuple

    Applying sorted() to the tuple produces a new list in ascending order. The original tuple remains unchanged.

  2. 2
    Reverse the tuple

    Applying reversed() to the tuple produces an iterator. That iterator yields the tuple's elements from the last element toward the first.

  3. 3
    Choose a concrete reverse result

    If a list or tuple is needed rather than an iterator, convert the result of reversed() to a list or tuple.

Result

The tuple stays unchanged; sorted() gives a new ascending list, and reversed() gives an iterator for reverse-order access.

Tupleoriginal orderNew listascending orderIteratorreverse orderTupleoriginal ordersorted()reversed()
How does data move from an original tuple into the values returned by sorted() and reversed(), and which object remains unchanged?

The word iterator matters for reversed(). Unlike sorted(), which directly returns a new list, reversed() returns an object that yields elements in reverse order one at a time. When a concrete sequence is required, convert that iterator to a list or tuple. This difference explains why reversed() is useful both for traversing data and for creating a stored reverse-order sequence when needed.

Methods and Functions Compared

sequence.sort()method callExisting listchanged in placesorted(sequence)function callNew listascending ordermodifiesreturns
What is the difference between asking a list to change itself and passing a sequence to a built-in function?
NeedPreferReason
The old list order is no longer neededsort() or reverse()These list methods modify the existing list in place
The original order must remain availablesorted() or reversed()These built-in functions do not modify the original sequence
The input is a tuplesorted() or reversed()Tuples do not provide in-place sort() or reverse() methods
A concrete ascending sequence is neededsorted()It returns a new list in ascending order
Reverse-order values should be generated one at a timereversed()It returns an iterator that yields elements in reverse order

Select the operation according to both the sequence type and whether the original order must be preserved.

Lists and Tuples in Practice

Listunchanged by built-insNew listsorted() resultNew listsorted() resultIteratorreversed() resultTupleunchangedIteratorreversed() resultsorted()reversed()sorted()reversed()
What happens to the original list or tuple, and what kind of result is produced, when sorted() or reversed() is applied?
In the real world

Imagine a list of items in the order they were received. If you need an alphabetical view but still need the received order later, sorted() gives you a new ascending list and preserves the original list. If you need to inspect the items from last received to first received without changing the list, reversed() gives you an iterator for that traversal. The same built-in functions can be used when the original data is a tuple.

Edge case

The result type is important. sorted() always returns a new list in ascending order, even when its input is a tuple. reversed() returns an iterator rather than a list or tuple. Convert that iterator to a list or tuple when a concrete sequence is required.

Mistakes Beginners Make

  • Common mistake

    Expecting a tuple to have sort() or reverse() methods

    What it looks like

    Treating a tuple exactly like a list because both are sequences

    Why it fails

    Those methods modify sequences in place, while tuples are immutable

    Do this instead

    Use sorted() for a new ascending list or reversed() for a reverse-order iterator

  • Common mistake

    Using sort() when the original list order is still needed

    What it looks like

    Sorting a list in place and then trying to recover its previous order

    Why it fails

    sort() changes the existing list

    Do this instead

    Use sorted() to create a new ascending list while preserving the original

  • Common mistake

    Assuming reversed() returns a list or tuple

    What it looks like

    Treating the direct result of reversed() as a concrete sequence

    Why it fails

    reversed() returns an iterator that yields elements in reverse order

    Do this instead

    Convert the iterator to a list or tuple when a concrete sequence is needed

  • Common mistake

    Assuming sorted() changes the original sequence

    What it looks like

    Calling sorted() and then expecting the input list or tuple itself to be in ascending order

    Why it fails

    sorted() returns a new list and leaves the original sequence unchanged

    Do this instead

    Use the returned list, or use an in-place list method when changing the original is intentional

Choose the Operation

MEDIUM

For each situation, choose the most suitable operation: sort(), reverse(), sorted(), or reversed(). First, decide whether the original order must remain available. Then consider whether the input is a list or an immutable sequence such as a tuple.

Hints
  • Use an in-place method only when changing an existing list is acceptable.
  • Use sorted() when you need a new ascending list.
  • Use reversed() when you need reverse-order access without modifying the original.
  • Remember that reversed() returns an iterator.
Worked example

Decision Walkthrough

You have a tuple and need its values in ascending order, but the tuple must remain unchanged.

  1. 1
    Check mutability

    The input is a tuple, so it cannot be modified in place.

  2. 2
    Check the desired result

    The desired result is ascending order, so sorted() is the matching built-in function.

  3. 3
    Check the result type

    The result will be a new list, while the original tuple remains unchanged.

Result

Choose sorted(). Do not use sort(), because tuples do not provide that in-place method.

The Working Rule

  1. 1
    Tuples are immutable, so they do not provide in-place sort() or reverse() methods.
  2. 2
    sort() and reverse() modify lists in place.
  3. 3
    sorted() works on sequences and returns a new list in ascending order without changing the original.
  4. 4
    reversed() works on sequences and returns an iterator that yields elements in reverse order without changing the original.
  5. 5
    Choose in-place list methods when the old order is no longer needed; choose sorted() or reversed() when the original sequence must be preserved.

Key Takeaways

  • Tuples cannot be changed in place, which is why they lack sort() and reverse() methods.
  • List methods sort() and reverse() change the existing list.
  • sorted() creates a new ascending list and leaves the input sequence unchanged.
  • reversed() creates an iterator that yields elements in reverse order; convert it to a list or tuple when needed.
  • The right choice depends on both the sequence type and whether the original order must be preserved.

Related concepts

More concepts starting with U