The reversed() function returns an iterator that accesses the elements of a sequence in reverse order.
Basic Syntax
reversed(sequence)It is commonly used with:
- Lists
- Tuples
- Strings
- Ranges
- Other reversible objects
1. Basic Example
numbers = [1, 2, 3, 4, 5]
result = reversed(numbers)
print(list(result))Output:
[5, 4, 3, 2, 1]reversed() does not directly return a list. It returns a reverse iterator.
2. Using reversed() with a for Loop
numbers = [1, 2, 3, 4, 5]
for number in reversed(numbers):
print(number)Output:
5
4
3
2
13. Reverse a List
names = ["Ram", "Sita", "Hari", "Gita"]
for name in reversed(names):
print(name)Output:
Gita
Hari
Sita
Ram4. Convert reversed() to a List
numbers = [10, 20, 30, 40]
result = list(reversed(numbers))
print(result)Output:
[40, 30, 20, 10]5. reversed() with Strings
Strings can also be reversed.
word = "Python"
result = reversed(word)
print("".join(result))Output:
nohtyPA simpler approach is:
word = "Python"
print("".join(reversed(word)))6. reversed() with a Tuple
numbers = (1, 2, 3, 4, 5)
result = tuple(reversed(numbers))
print(result)Output:
(5, 4, 3, 2, 1)7. reversed() with range()
numbers = range(1, 6)
for number in reversed(numbers):
print(number)Output:
5
4
3
2
18. Reverse a List Without Changing the Original
numbers = [1, 2, 3, 4, 5]
result = list(reversed(numbers))
print("Original:", numbers)
print("Reversed:", result)Output:
Original: [1, 2, 3, 4, 5]
Reversed: [5, 4, 3, 2, 1]This is useful because the original list remains unchanged.
9. reversed() vs reverse()
Python lists have a .reverse() method.
numbers = [1, 2, 3, 4, 5]
numbers.reverse()
print(numbers)Output:
[5, 4, 3, 2, 1].reverse() changes the original list.
With reversed():
numbers = [1, 2, 3, 4, 5]
result = list(reversed(numbers))
print(numbers)
print(result)Output:
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]Main difference
| Method | Original changed? | Returns |
|---|---|---|
list.reverse() | Yes | None |
reversed() | No | Reverse iterator |
10. reversed() vs sorted(reverse=True)
These are also different.
reversed() simply changes the direction:
numbers = [5, 2, 8, 1, 3]
print(list(reversed(numbers)))Output:
[3, 1, 8, 2, 5]It does not sort the values.
sorted(reverse=True) sorts first and then puts them in descending order:
numbers = [5, 2, 8, 1, 3]
print(sorted(numbers, reverse=True))Output:
[8, 5, 3, 2, 1]Remember:
reversed()
↓
Reverse existing order
sorted(reverse=True)
↓
Sort in descending order11. Reverse Using for Loop
names = ["Ram", "Sita", "Hari", "Gita"]
for index in range(len(names) - 1, -1, -1):
print(names[index])Output:
Gita
Hari
Sita
RamHowever, reversed() is usually cleaner:
for name in reversed(names):
print(name)12. reversed() with enumerate()
You can combine reversed() and enumerate().
names = ["Ram", "Sita", "Hari", "Gita"]
for index, name in enumerate(reversed(names), start=1):
print(index, name)Output:
1 Gita
2 Hari
3 Sita
4 Ram13. Practical Example : Recent Courses
Suppose courses are stored in the order they were added:
courses = [
"Python",
"Django",
"MERN",
"Data Science"
]
for course in reversed(courses):
print(course)Output:
Data Science
MERN
Django
PythonThis can be useful when displaying the most recently added item first.
14. Practical Example : Transaction History
transactions = [
"Payment 1",
"Payment 2",
"Payment 3",
"Payment 4"
]
for transaction in reversed(transactions):
print(transaction)Output:
Payment 4
Payment 3
Payment 2
Payment 115. Practical Example : Student Records
students = ["Ram", "Sita", "Hari", "Gita"]
for roll, student in enumerate(
reversed(students),
start=1
):
print(roll, student)Output:
1 Gita
2 Hari
3 Sita
4 RamNote that these are new positions in the reversed sequence, not the students' original roll numbers.
16. Practical Example : Reverse Words
sentence = "Python is easy to learn"
words = sentence.split()
result = " ".join(reversed(words))
print(result)Output:
learn to easy is Python17. Practical Example : Palindrome
A palindrome reads the same forward and backward.
word = "madam"
reversed_word = "".join(
reversed(word)
)
if word == reversed_word:
print("Palindrome")
else:
print("Not a palindrome")Output:
Palindrome18. reversed() with a List of Dictionaries
students = [
{"name": "Ram", "marks": 75},
{"name": "Sita", "marks": 85},
{"name": "Hari", "marks": 65}
]
for student in reversed(students):
print(student["name"])Output:
Hari
Sita
Ram19. reversed() with sorted()
You can combine the concepts:
numbers = [5, 2, 8, 1, 3]
result = reversed(
sorted(numbers)
)
print(list(result))Output:
[8, 5, 3, 2, 1]But this is equivalent to:
result = sorted(
numbers,
reverse=True
)
print(result)The second version is more direct.
20. Important Points to Remember
Basic syntax
reversed(sequence)Convert to list
list(reversed(numbers))Use in a loop
for item in reversed(items):
print(item)Reverse a string
"".join(reversed(word))Main characteristic
reversed() does not sort the data. It simply iterates through the existing sequence from the last element to the first.
Easy way to remember
sorted()
↓
Arrange values
reversed()
↓
Go backward