Recall & Review
beginner Click to reveal answer
What does the
reversed() function do in Python?It returns an iterator that accesses the given sequence in reverse order without changing the original sequence.
beginner Click to reveal answer
Can
reversed() be used on any iterable in Python?No,
reversed() works only on sequences that support indexing and have a defined length, like lists, tuples, and strings.beginner Click to reveal answer
How do you convert the result of
reversed() into a list?Use the
list() function around reversed(), like list(reversed(my_list)).beginner Click to reveal answer
What is the output of
list(reversed('hello'))?['o', 'l', 'l', 'e', 'h'] - the characters of the string 'hello' in reverse order as a list.
beginner Click to reveal answer
Does
reversed() modify the original sequence?No, it does not change the original sequence; it only provides a reversed iterator.
Which of these can
reversed() be used on?✗ Incorrect
reversed() works on sequences like lists, not on dictionaries, sets, or generators.What type does
reversed() return?✗ Incorrect
reversed() returns an iterator that you can loop over or convert to a list.How do you get a reversed list from a tuple
t?✗ Incorrect
Only
list(reversed(t)) produces a reversed list from a tuple t. reversed(list(t)) returns an iterator and t[::-1] returns a reversed tuple.What happens if you try
reversed() on a set?✗ Incorrect
reversed() requires a sequence with indexing; sets do not support this, so it raises an error.Which of these is a correct way to print characters of a string in reverse?
✗ Incorrect
Slicing with [::-1] and converting reversed() to list both print reversed characters.
Explain how the
reversed() function works and when you would use it.Think about how you might want to read a list or string backwards without changing it.
You got /4 concepts.
Describe the difference between using
reversed() and slicing with [::-1] to reverse a sequence.Consider the type of result and performance differences.
You got /4 concepts.
