0
0
Pythonprogramming~10 mins

reversed() function in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - reversed() function
Input iterable
Call reversed()
Create reverse iterator
Iterate over reversed elements
Output elements in reverse order
End iteration
The reversed() function takes an iterable and returns an iterator that yields elements in reverse order until all elements are exhausted.
Execution Sample
Python
items = [1, 2, 3]
rev_iter = reversed(items)
for x in rev_iter:
    print(x)
This code reverses the list [1, 2, 3] and prints each element in reverse order.
Execution Table
StepActionIterator StateOutput
1Create reversed iterator from [1, 2, 3][3, 2, 1]
2Get next element from iterator[2, 1]3
3Get next element from iterator[1]2
4Get next element from iterator[]1
5Get next element from iterator[]StopIteration raised
💡 Iterator exhausted, no more elements to return
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
items[1, 2, 3][1, 2, 3][1, 2, 3][1, 2, 3][1, 2, 3][1, 2, 3]
rev_iterNoneIterator over [3, 2, 1]Iterator over [2, 1]Iterator over [1]Iterator emptyIterator exhausted
Key Moments - 3 Insights
Why does reversed() return an iterator and not a list?
reversed() returns an iterator to save memory and allow lazy evaluation, as shown in execution_table steps 1-5 where elements are produced one by one.
What happens if you try to get next element after the iterator is exhausted?
As shown in step 5 of execution_table, a StopIteration exception is raised indicating no more elements are available.
Can reversed() be used on any iterable?
No, reversed() requires a sequence (like list, tuple, or string) that supports __reversed__ or __len__ and __getitem__, otherwise it raises an error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at Step 3?
A3
B1
C2
DStopIteration
💡 Hint
Check the Output column at Step 3 in execution_table
At which step does the iterator become empty?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the Iterator State column to see when it becomes []
If the input list was empty, what would happen when calling reversed()?
AStopIteration immediately on next()
BIterator with elements
CError raised
DOutput the original list
💡 Hint
Consider what happens when reversed() is called on an empty sequence and check exit_note
Concept Snapshot
reversed(iterable)
- Returns an iterator that yields elements in reverse order
- Works on sequences supporting __reversed__ or __getitem__
- Does not create a reversed list, saves memory
- Raises StopIteration when done
- Use in for-loops or with next() to access reversed elements
Full Transcript
The reversed() function takes a sequence like a list and returns an iterator that produces elements from the end to the start. When you call reversed() on [1, 2, 3], it creates an iterator starting at 3. Each call to next() returns the next element backward: 3, then 2, then 1. After all elements are returned, next() raises StopIteration to signal the end. The original list remains unchanged. reversed() is efficient because it does not copy the list but yields elements one by one. It only works on sequences that support reverse iteration. If you try reversed() on an empty list, the iterator is empty and raises StopIteration immediately.