Remember
reversed() returns an iterator over a reversible sequence in reverse order. It does not change the original sequence.
reversed() returns an iterator over a reversible sequence in reverse order. It does not change the original sequence.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
items = [3, 2, 5]
print(list(reversed(items)))
print(items)Top-level scope
[3, 2, 5]reversed() returns an iterator over a reversible sequence in reverse order. It does not change the original sequence.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
items = [3, 2, 5]
print(list(reversed(items)))
print(items)
|
v
DISPLAYED RESULT
[5, 2, 3]
[3, 2, 5]
FINAL BINDINGS
items --> object 1 (list)
object 1: [3, 2, 5]Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
items = [3, 2, 5]
print(list(reversed(items)))
print(items)Top-level scope
[3, 2, 5]The example has finished. Compare the final output with the values and references above.
[5, 2, 3] [3, 2, 5]
What do you expect this program to print?
items = [3, 2, 5]
print(list(reversed(items)))
print(items)reversed() FunctionThe reversed() function in Python returns an iterator that accesses the given sequence in the reverse order. It is commonly used to loop through lists or other sequences from the end to the beginning.
reversed(sequence)
sequence – A sequence that supports reverse iteration (like list, tuple, string, or a user-defined class with __reversed__()).numbers = [1, 2, 3, 4, 5]
for num in reversed(numbers):
print(num)
5
4
3
2
1
text = "hello"
for char in reversed(text):
print(char)
o
l
l
e
h
items = ['a', 'b', 'c']
rev_items = list(reversed(items))
print(rev_items)
['c', 'b', 'a']
reversed() does not return a reversed list directly — it returns an iterator.list() or tuple() if you want to store or print the reversed result directly.reversed() on non-sequence types like sets or dictionaries will raise a TypeError.reversed(list(my_set))In interviews, use reversed() when you're asked to check for palindromes or reverse iteration in loops. It's cleaner and more readable than using indexes.
reversed() returns an iterator for reverse traversal.__reversed__().list(reversed(...)) to get a reversed list.Write a program that checks if a word is a palindrome (reads the same backward).
word = input("Enter a word: ")
if list(word) == list(reversed(word)):
print("It's a palindrome!")
else:
print("Not a palindrome.")
Connect what you noticed in the example with the idea below.
reversed() returns an iterator over a reversible sequence in reverse order. It does not change the original sequence.
reversed() always modifies its source list.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.