Reversed in Python | Python reversed() Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The built-in reversed function in Python is used to reverse a sequence like a list, string, or tuple by creating a reverse iterator of the given sequence.

Syntax of reversed() in Python

The syntax of reversed function in Python is:

Parameters of reversed() in Python

The reversed function takes a single parameter:

  • seq: the name of the sequence (list, str, tuple) whose reverse iterator is needed.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Return Values of reversed() in Python

Return Type: <class 'reversed'>

reversed in Python returns an iterator that accesses the given sequence in the reverse order.

Exceptions of reversed() in Python

  • StopIteration: The reversed function in Python will raise StopIteration error if we call next method to access an element even after getting the last element.

Example

Output of reversed in Python

What is Reversed in Python?

reversed is a built-in function in Python used to get a reversed iterator of a sequence.

reversed function is similar to the iter() method but in reverse order.

An iterator is an object used to iterate over an iterable.

  • We can generate an iterator object using the iter method on an iterable.
  • Use the next function to get a value from an iterable using an iterator.

Example:

Output:

If you aren't familiar with iterators, you can go through this article on Scaler Topics: Iterators in Python.

what is reversed in python

We can use the reversed function with any sequence or class that implements the __reverse()__ method.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

More Examples

Example 1: Using reversed() in string, tuple, list, and range

Output:

Explanation:

We used the reversed function with a list, string, tuple, and a range object. As we can't see the elements inside an iterator object directly, we need to convert the return value to either a list or tuple so that we can see the output. Printing a reversed iterator is not meaningful.

Example 2: reversed() with for loop

Output:

Explanation:

We can use the reversed function with a for a loop because it returns an iterator. In this example, we used the reversed function to get the reverse of my_string then we used it inside a for loop to print each character one by one.

Example 3: reversed() in custom objects()

Output:

Explanation:

As stated before, we can use reversed with any class that implements the __reversed__ method.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Example 4: Example of Exception in reversed() function

Output:

Explanation:

We can use the next function to access a value from an iterator. Once the iterator is exhausted, the next function will raise a StopIteration error.

In this example, we reversed a list with three elements and then accessed each element with the next function after accessing the 1, the last element in the reversed list. The iterator gets exhausted. That's why using the next function in the fourth print statement raises the StopIteration error.

Conclusion

  • The built-in reversed function in Python is used to reverse a sequence.
  • Reversed function in Python takes a sequence as an argument and returns a reverse iterator over the elements.
  • We can use the reversed function with any sequence or class that implements the reverse() method.
  • The reversed function in Python raises a StopIteration error when all the elements are exhausted.

See Also:

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more