Bird
Raised Fist0
Pythonprogramming~20 mins

reversed() function in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
Reversed Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of reversed() on a string
What is the output of this Python code?
Python
s = 'hello'
result = ''.join(reversed(s))
print(result)
AError: reversed() cannot be used on strings
Bolleh
C['o', 'l', 'l', 'e', 'h']
Dhello
Attempts:
2 left
๐Ÿ’ก Hint
reversed() returns an iterator that goes through the input backwards.
โ“ Predict Output
intermediate
2:00remaining
Using reversed() on a list
What is the output of this code snippet?
Python
lst = [1, 2, 3, 4]
rev_lst = list(reversed(lst))
print(rev_lst)
A[1, 2, 3, 4]
B[1, 4, 2, 3]
C[4, 3, 2, 1]
DError: reversed() only works on strings
Attempts:
2 left
๐Ÿ’ก Hint
reversed() works on any sequence, including lists.
โ“ Predict Output
advanced
2:00remaining
Output of reversed() on a dictionary keys view
What happens when you run this code?
Python
d = {'a': 1, 'b': 2, 'c': 3}
print(list(reversed(d.keys())))
ATypeError: 'dict_keys' object is not reversible
B['a', 'b', 'c']
C['c', 'b', 'a']
D['a', 'c', 'b']
Attempts:
2 left
๐Ÿ’ก Hint
Check if dict_keys supports reversed() directly.
โ“ Predict Output
advanced
2:00remaining
Reversing a range object
What is the output of this code?
Python
r = range(1, 5)
print(list(reversed(r)))
A[4, 3, 2, 1]
B[1, 2, 3, 4]
CTypeError: 'range' object is not reversible
D[5, 4, 3, 2]
Attempts:
2 left
๐Ÿ’ก Hint
range objects support reversed() because they are sequences.
๐Ÿง  Conceptual
expert
3:00remaining
Behavior of reversed() on custom iterable
Consider this class: class CountDown: def __init__(self, start): self.start = start def __iter__(self): n = self.start while n > 0: yield n n -= 1 What happens if you call reversed(CountDown(3))?
A[] (empty list)
B[3, 2, 1]
C[1, 2, 3]
DTypeError: object is not reversible
Attempts:
2 left
๐Ÿ’ก Hint
reversed() requires __reversed__ or __len__ and __getitem__ methods.

Practice

(1/5)
1. What does the reversed() function do in Python?
easy
A. It sorts the items of a sequence in ascending order.
B. It returns an iterator that goes through the items of a sequence backwards.
C. It removes duplicate items from a sequence.
D. It changes the original sequence to its reversed form.

Solution

  1. Step 1: Understand the purpose of reversed()

    The reversed() function returns an iterator that accesses the elements of a sequence in reverse order.
  2. Step 2: Check if original sequence changes

    The original sequence remains unchanged; reversed() only provides a way to loop backwards.
  3. Final Answer:

    It returns an iterator that goes through the items of a sequence backwards. -> Option B
  4. Quick 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
A. for ch in reversed('hello'): print(ch)
B. for ch in reverse('hello'): print(ch)
C. for ch in 'hello'.reversed(): print(ch)
D. for ch in reversed['hello']: print(ch)

Solution

  1. Step 1: Identify correct function syntax

    reversed() is a built-in function called with parentheses and a sequence inside, like reversed('hello').
  2. Step 2: Check loop syntax

    The for loop correctly iterates over the reversed iterator returned by reversed().
  3. Final Answer:

    for ch in reversed('hello'): print(ch) -> Option A
  4. Quick 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
A. Error: reversed() cannot be used on lists
B. [1, 2, 3, 4]
C. [4, 3, 2, 1]
D. [1, 4, 3, 2]

Solution

  1. 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.
  2. Step 2: Convert iterator to list

    Using list() on the reversed iterator collects all items into a new list in reversed order.
  3. Final Answer:

    [4, 3, 2, 1] -> Option C
  4. Quick 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
A. It prints a reversed iterator object, not the reversed string.
B. It prints a reversed string directly.
C. It causes a syntax error because reversed() needs a list.
D. It modifies my_str permanently.

Solution

  1. Step 1: Understand what reversed() returns

    reversed(my_str) returns an iterator, not a string.
  2. Step 2: Printing the iterator shows its object info

    Printing rev_str directly prints something like <reversed object at ...>, not the reversed characters.
  3. Final Answer:

    It prints a reversed iterator object, not the reversed string. -> Option A
  4. Quick 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
A. new_s = s.reverse()
B. new_s = reversed(s)
C. new_s = s[::-1]
D. new_s = ''.join(reversed(s))

Solution

  1. Step 1: Use reversed() to get reversed iterator

    reversed(s) returns an iterator over characters of s in reverse order.
  2. Step 2: Join characters into a new string

    ''.join(reversed(s)) combines the reversed characters into a new string.
  3. Final Answer:

    new_s = ''.join(reversed(s)) -> Option D
  4. Quick 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()