Understanding Iterators and Generators
The word iterator matters for reversed().
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.
| Operation | Works with | Result or effect | Original sequence |
|---|---|---|---|
| sort() | Lists | Sorts the existing list in place | Changed |
| reverse() | Lists | Reverses the existing list in place | Changed |
| sorted() | Sequences such as lists, tuples, and strings | Returns a new list in ascending order | Unchanged |
| reversed() | Sequences such as lists, tuples, and strings | Returns an iterator yielding elements in reverse order | Unchanged |
The operation determines whether the original sequence is changed and what kind of result is produced.
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.
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.
- 1Sort the tuple
Applying
sorted()to the tuple produces a new list in ascending order. The original tuple remains unchanged. - 2Reverse the tuple
Applying
reversed()to the tuple produces an iterator. That iterator yields the tuple's elements from the last element toward the first. - 3Choose 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.
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
| Need | Prefer | Reason |
|---|---|---|
| The old list order is no longer needed | sort() or reverse() | These list methods modify the existing list in place |
| The original order must remain available | sorted() or reversed() | These built-in functions do not modify the original sequence |
| The input is a tuple | sorted() or reversed() | Tuples do not provide in-place sort() or reverse() methods |
| A concrete ascending sequence is needed | sorted() | It returns a new list in ascending order |
| Reverse-order values should be generated one at a time | reversed() | 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
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
Common mistake Expecting a tuple to have sort() or reverse() methods
What it looks likeTreating a tuple exactly like a list because both are sequences
Why it failsThose methods modify sequences in place, while tuples are immutable
Do this insteadUse 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 likeSorting a list in place and then trying to recover its previous order
Why it failssort()changes the existing listDo this insteadUse sorted() to create a new ascending list while preserving the original
Common mistake Assuming reversed() returns a list or tuple
What it looks likeTreating the direct result of reversed() as a concrete sequence
Why it failsreversed()returns an iterator that yields elements in reverse orderDo this insteadConvert the iterator to a list or tuple when a concrete sequence is needed
Common mistake Assuming sorted() changes the original sequence
What it looks likeCalling sorted() and then expecting the input list or tuple itself to be in ascending order
Why it failssorted()returns a new list and leaves the original sequence unchangedDo this insteadUse the returned list, or use an in-place list method when changing the original is intentional
Choose the Operation
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.
- 1Check mutability
The input is a tuple, so it cannot be modified in place.
- 2Check the desired result
The desired result is ascending order, so
sorted()is the matching built-in function. - 3Check 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
- 1Tuples are immutable, so they do not provide in-place
sort()orreverse()methods. - 2
sort()andreverse()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. - 5Choose in-place list methods when the old order is no longer needed; choose
sorted()orreversed()when the original sequence must be preserved.
Key Takeaways
- Tuples cannot be changed in place, which is why they lack
sort()andreverse()methods. - List methods
sort()andreverse()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.