What does reversed () do in python?

Hello, hope you all are doing well! In this article, we will be understanding the working of an in-built function — Python reversed() function.


Working of Python reversed() function

Python serves us with a huge count of in-built functions to deal and manipulate the data.

One such function is Python reversed() function.

Python reversed() function processes the input data values in reverse order. The reversed() function works with lists, string, etc. and returns an iterator by processing the sequence of given data elements in reverse order.

Thus, it can be said that Python reversed() function can be used for reverse screening of data values of any data structure.

Having understood the working of reversed() function, let us now focus on the syntax of the same.


Syntax of Python reversed() function

As mentioned above, Python reversed() function iterates over the data values in a reverse order.

Syntax:

The reversed() function returns a reverse iterator i.e. it returns an iterator that fetches and represents the data values in a reversed order.

Now, let us understand the implementation of Python reversed() function in the below section.


Implementing the reversed() function through examples

In the below example, we have created a list of numeric data values and passed it to the reversed() function.

Example 1:

lst = [10, 15, 43, 56]
rev = reversed(lst)
print(rev)
print(list(rev))

The reversed() function returns the reverse iterator as seen in the output when we try to execute — print(rev).

Further, in order to access the data values manipulated from the reversed() function, we use the list() function to print the data values using the reverse iterator.

Output:

<list_reverseiterator object at 0x00000272A6901390>
[56, 43, 15, 10]

In this example, we have passed string values to the list and then passed it to the reversed() function.

Example 2:

lst = ['Python','Java','C++','ML']
rev = reversed(lst)
print(list(rev))

Output:

['ML', 'C++', 'Java', 'Python']

Example 3:

tuple_data = ('Python','Java','C++','ML',100,21)
rev = reversed(tuple_data)
print(tuple(rev))

Now, we have created a tuple of data values and passed it to the reversed() function to reverse the data values.

Output:

(21, 100, 'ML', 'C++', 'Java', 'Python')

Example 4:

data = list(range(1,5))
print("Original Data: ",data)
rev = list(reversed(data))
print("Reversed Data: ",rev)

Python reversed() function can be used along with range() function to return an iterator of sequence of data value in a reversed order.

Output:

Original Data:  [1, 2, 3, 4]
Reversed Data:  [4, 3, 2, 1]


Conclusion

By this, we have reached the end of this topic. Feel free to comment below in case, you come across any doubt. Continue to follow us for more such posts!


References

  • Python reversed() function — JournalDev

What does reversed () do in python?

The reversed() method returns the iterator that accesses the given sequence in the reverse order.

Python reversed() is an inbuilt function that returns an iterator that accesses the given sequence in the reverse order. The iter() function returns an iterator object. The list.reverse() method reverses a List.

Syntax

reversed(seq)

Parameters

The sequence parameter is required, and it is an iterable object.

It could be an object that supports sequence protocol (__len__() and __getitem__() methods) as a tuple, string, list, or range.

See the following example.

# app.py

# for string
string = 'AppDividend'
print(list(reversed(string)))

# for tuple
tuple = ('k', 'r', 'u', 'n', 'a', 'l')
print(list(reversed(tuple)))

# for range
range = range(19, 24)
print(list(reversed(range)))

# for list
listA = [19, 21, 46, 29, 6]
print(list(reversed(listA)))

See the following example.

What does reversed () do in python?

In the above code example, we have used the string, range, list, and tuple data types and structures.

How reversed works for custom objects

Let’s see an example of reversed with custom objects.

# app.py

class AppDividend:
    title = ['a', 'p', 'p', 'd', 'i', 'v', 'i', 'd', 'e', 'n', 'd']

    def __reversed__(self):
        return reversed(self.title)

obj = AppDividend()
print(list(reversed(obj)))

See the output.

What does reversed () do in python?

We can supply the custom object as a reversed() function argument if it has the __reversed__() method or supports a sequence protocol.

We need to implement a __len__() method and the __getItem__() method with integer arguments starting at 0 to support the sequence protocol.

So, Python reversed() function returns the reverse iterator. The seq must be the object which has the __reversed__() method or supports a sequence protocol (the __len__() method and a __getItem__() method with the integer arguments starting at 0).

By using the reversed() function and a string join() function, you can reverse the string. For example, if we have the string “ABCDEF” and then if you apply the join() function, then you will get the reverse string. For more detail, see the how to reverse string example.

See the below example.

# app.py

string="ABCDEF"		
print(''.join(reversed(string)))

See the output.

What does reversed () do in python?

That’s it for this tutorial.

How does Python Reverse () work?

Python reversed() function The reversed() function allows us to process the items in a sequence in reverse order. It accepts a sequence and returns an iterator. A sequence list string, list, tuple etc. To produce the result at once we have wrapped reversed() in a list() call.

What is reverse in Python with example?

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.

What is __ reversed __ in Python?

Python reversed() method The reversed() method returns the reversed iterator of the given sequence. It is the same as the iter() method but in reverse order. Internally, it calls the __reversed__() method of the sequence class.

Does Reverse () work on strings?

These features allow you to use slicing to directly generate a copy of a given string in reverse order. The second option is to use the built-in function reversed() to create an iterator that yields the characters of an input string in reverse order. ... Reversing Strings Through Slicing..