Lessons
Courses

`reversed()` Function in Python [ English ]

1. Introduction

The reversed() function in Python is a built-in function used to return an iterator that accesses the elements of a sequence in reverse order. It does not modify the original sequence; instead, it produces a reverse iterator that allows elements to be retrieved from the last element to the first.

The reversed() function works with sequence types such as lists, tuples, strings, and ranges.

2. Syntax

reversed(sequence)

Parameter

  • sequence – A sequence object such as a list, tuple, string, or range whose elements need to be accessed in reverse order.

Return Value

  • Returns a reverse iterator object.

3. Example with a List

numbers = [1, 2, 3, 4, 5] rev = reversed(numbers) print(list(rev))

Output

[5, 4, 3, 2, 1]

Explanation

  • reversed(numbers) creates a reverse iterator.
  • Converting it into a list using list() shows the elements in reverse order.

4. Using reversed() in a Loop

numbers = [10, 20, 30, 40] for num in reversed(numbers): print(num)

Output

40302010

Explanation

  • The loop retrieves the elements of the list starting from the last element.

5. Example with a String

text = "Python" for char in reversed(text): print(char)

Output

nohtyP

Explanation

  • Each character of the string is accessed in reverse order.

6. Example with a Tuple

data = (1, 2, 3) print(list(reversed(data)))

Output

[3, 2, 1]

Explanation

  • The tuple is reversed and the result is returned as a list.

7. Example with range()

for i in reversed(range(5)): print(i)

Output

43210

Explanation

  • The numbers produced by range(5) are accessed in reverse order.

8. Difference Between reversed() and Slicing

Using reversed()

numbers = [1, 2, 3, 4]print(list(reversed(numbers)))

Using slicing

numbers = [1, 2, 3, 4]print(numbers[::-1])
Featurereversed()Slicing
Return typeIteratorList
Memory usageMore efficientCreates a new list
UsageIterationDirect reversed copy

9. Summary

  • reversed() is a built-in Python function used to iterate through a sequence in reverse order.
  • It returns a reverse iterator object.
  • It works with lists, tuples, strings, and ranges.
  • The original sequence remains unchanged.
  • It is commonly used in loops and reverse traversal of sequences.