Python reversed
Visit the current history from newest to oldest without rearranging it.
A simple example
reversed
- Start with A, B, C
- Do this Read in reverse order
- Result C, B, A
Syntax
reversed(receipts)
Run these examples in Python
Save one example at a time as lesson.py. Use Python 3.9 or later.
python3 lesson.pyRead a receipt history newest first
A shop archives receipts in the order they were issued. The returns counter needs the newest receipt at the top of its screen without rearranging the archive for the accounting team.
receipts = ["R1", "R2", "R3"]
print(", ".join(reversed(receipts)))
R3, R2, R1
join consumes the iterator in reverse receipt order.
Prepare the next workshop roster
A teacher reads a workshop roster in arrival order. The coordinator needs to visit the entries in reverse order on the return trip. Read the order from the final entry back to the first.
Work from the delivery round
A courier has three labelled stops in route order. The coordinator needs to visit the entries in reverse order on the return trip. Read the order from the final entry back to the first.
What to watch for
The result is an iterator. Materialize it if the caller needs a separate reusable list.