The reversed() function helps you look at items in a list or string from the end to the start. It makes it easy to go backwards without changing the original order.
reversed() function in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
reversed(sequence)
sequence can be a list, string, tuple, or any object that supports reverse iteration.
reversed() returns an iterator, so you often convert it to a list or use it in a loop.
Examples
Python
for char in reversed('hello'): print(char, end='')
Python
numbers = [1, 2, 3, 4] rev_numbers = list(reversed(numbers)) print(rev_numbers)
Python
tuple_data = (10, 20, 30) for item in reversed(tuple_data): print(item)
Sample Program
This program reverses the string 'python' and prints 'nohtyp'.
Python
word = 'python' reversed_word = ''.join(reversed(word)) print(reversed_word)
Important Notes
The original sequence is not changed by reversed().
To see the reversed items all at once, convert the iterator to a list or string.
Summary
reversed() lets you go through items backwards without changing the original.
It works with strings, lists, tuples, and more.
You usually use it in loops or convert it to a list or string to see all reversed items.
Practice
1. What does the
reversed() function do in Python?easy
Solution
Step 1: Understand the purpose of reversed()
Thereversed()function returns an iterator that accesses the elements of a sequence in reverse order.Step 2: Check if original sequence changes
The original sequence remains unchanged;reversed()only provides a way to loop backwards.Final Answer:
It returns an iterator that goes through the items of a sequence backwards. -> Option BQuick Check:
reversed() returns reversed iterator [OK]
Hint: Remember: reversed() does not change original, just reads backwards [OK]
Common Mistakes:
- Thinking reversed() sorts the sequence
- Assuming reversed() modifies the original sequence
- Confusing reversed() with removing duplicates
2. Which of the following is the correct way to use
reversed() to print characters of a string backwards?easy
Solution
Step 1: Identify correct function syntax
reversed()is a built-in function called with parentheses and a sequence inside, likereversed('hello').Step 2: Check loop syntax
The for loop correctly iterates over the reversed iterator returned byreversed().Final Answer:
for ch in reversed('hello'): print(ch) -> Option AQuick Check:
Use reversed() with parentheses and a sequence [OK]
Hint: Use reversed() with parentheses, not square brackets or dot calls [OK]
Common Mistakes:
- Using square brackets instead of parentheses
- Calling reversed as a method on string
- Typing reverse instead of reversed
3. What is the output of this code?
print(list(reversed([1, 2, 3, 4])))
medium
Solution
Step 1: Apply reversed() to the list
reversed([1, 2, 3, 4])returns an iterator that goes through the list backwards: 4, 3, 2, 1.Step 2: Convert iterator to list
Usinglist()on the reversed iterator collects all items into a new list in reversed order.Final Answer:
[4, 3, 2, 1] -> Option CQuick Check:
list(reversed([1,2,3,4])) = [4,3,2,1] [OK]
Hint: Wrap reversed() with list() to see reversed items as a list [OK]
Common Mistakes:
- Expecting reversed() to return a list directly
- Confusing reversed() with sort()
- Thinking reversed() modifies original list
4. What is wrong with this code?
my_str = 'abc' rev_str = reversed(my_str) print(rev_str)
medium
Solution
Step 1: Understand what reversed() returns
reversed(my_str)returns an iterator, not a string.Step 2: Printing the iterator shows its object info
Printingrev_strdirectly prints something like<reversed object at ...>, not the reversed characters.Final Answer:
It prints a reversed iterator object, not the reversed string. -> Option AQuick Check:
reversed() returns iterator, print shows object [OK]
Hint: Convert reversed() result to list or string before printing [OK]
Common Mistakes:
- Expecting reversed() to return a string
- Trying to print reversed() result directly
- Thinking reversed() modifies original string
5. How can you use
reversed() to create a new string that is the reverse of the original string s = 'Python'?hard
Solution
Step 1: Use reversed() to get reversed iterator
reversed(s)returns an iterator over characters ofsin reverse order.Step 2: Join characters into a new string
''.join(reversed(s))combines the reversed characters into a new string.Final Answer:
new_s = ''.join(reversed(s)) -> Option DQuick Check:
Use join() with reversed() to build reversed string [OK]
Hint: Use ''.join(reversed(s)) to reverse strings easily [OK]
Common Mistakes:
- Assigning reversed(s) directly to string variable
- Using s.reverse() which is invalid for strings
- Confusing slicing with reversed()
