0
0
Pythonprogramming~5 mins

reversed() function in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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.
Click to reveal answer
beginner
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.
Click to reveal answer
beginner
How do you convert the result of reversed() into a list?
Use the list() function around reversed(), like list(reversed(my_list)).
Click to reveal answer
beginner
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.
Click to reveal answer
beginner
Does reversed() modify the original sequence?
No, it does not change the original sequence; it only provides a reversed iterator.
Click to reveal answer
Which of these can reversed() be used on?
AA dictionary
BA list
CA set
DA generator
What type does reversed() return?
AA list
BA string
CA tuple
DAn iterator
How do you get a reversed list from a tuple t?
Alist(reversed(t))
Breversed(list(t))
Ct[::-1]
DAll of the above
What happens if you try reversed() on a set?
AIt returns a reversed iterator
BIt returns the set unchanged
CIt raises a TypeError
DIt converts the set to a list automatically
Which of these is a correct way to print characters of a string in reverse?
ABoth B and C
Bprint('abc'[::-1])
Cprint(list(reversed('abc')))
Dprint(reversed('abc'))
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.