Concepts / Understanding Iterators and Generators

Understanding Iterators and Generators

Tuples are immutable and therefore do not provide .sort() or .reverse() methods. These methods modify sequences in place, which violates immutability.

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

sort() changessorted() producesListoriginal orderListsorted orderListoriginal orderNew listsorted order
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

Why Tuples Have No In-Place Methods

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.

supportssupportscan usecan useListmutablesort()in-place changesorted()new listreverse()in-place changeTupleimmutablereversed()iterator
Why can a list provide methods that change its order while a tuple cannot provide equivalent in-place methods?

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

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.

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

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.

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.

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

sorted()reversed()Tupleoriginal orderNew listascending orderIteratorreverse orderTupleoriginal order
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

modifiesreturnssequence.sort()method callExisting listchanged in placesorted(sequence)function callNew listascending order
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

Lists and Tuples in Practice

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

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.

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

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

    Those methods modify sequences in place, while tuples are immutable

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

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

    sort() changes the existing list

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

  • Assuming reversed() returns a list or tuple

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

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

  • Assuming sorted() changes the original sequence

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

    Fix: 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.

Decision Walkthrough

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

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

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

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

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

The Working Rule

  1. Tuples are immutable, so they do not provide in-place sort() or reverse() methods.
  2. sort() and reverse() modify lists in place.
  3. sorted() works on sequences and returns a new list in ascending order without changing the original.
  4. reversed() works on sequences and returns an iterator that yields elements in reverse order without changing the original.
  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.