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
Using the reversed() Function in Python
๐ Scenario: You are organizing a list of guests for a party. You want to see the list in reverse order to plan the seating arrangement from the last guest to the first.
๐ฏ Goal: Learn how to use the reversed() function to reverse the order of items in a list and display the reversed list.
๐ What You'll Learn
Create a list of guest names with exact values
Use a variable to hold the reversed list
Use the reversed() function to reverse the list
Print the reversed list
๐ก Why This Matters
๐ Real World
Reversing lists is useful when you want to process items from the end to the start, like undoing actions or showing recent items first.
๐ผ Career
Understanding how to reverse data helps in data processing, user interface design, and algorithms where order matters.
Progress0 / 4 steps
1
Create the guest list
Create a list called guests with these exact names in order: 'Alice', 'Bob', 'Charlie', 'Diana', 'Ethan'.
Python
Hint
Use square brackets [] to create a list and separate names with commas.
2
Create a variable for the reversed list
Create a variable called reversed_guests and set it to the result of reversed(guests).
Python
Hint
The reversed() function returns an iterator. Assign it to a variable.
3
Convert reversed iterator to a list
Convert reversed_guests to a list and assign it back to reversed_guests using list().
Python
Hint
Use list() to turn the reversed iterator into a list you can print.
4
Print the reversed guest list
Print the variable reversed_guests to display the reversed list of guests.
Python
Hint
Use print() to show the reversed list on the screen.
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
Step 1: Understand the purpose of reversed()
The reversed() 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 B
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
Step 1: Identify correct function syntax
reversed() is a built-in function called with parentheses and a sequence inside, like reversed('hello').
Step 2: Check loop syntax
The for loop correctly iterates over the reversed iterator returned by reversed().
Final Answer:
for ch in reversed('hello'): print(ch) -> Option A
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
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
Using list() on the reversed iterator collects all items into a new list in reversed order.
Final Answer:
[4, 3, 2, 1] -> Option C
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]