## Reverse Vs Reversed Function In Python – Comparison

Lists are one of the in-built data types in Python used to store the collections of data. It is used to work with a sequence of data. Besides data types, Python has numerous standard functions for performing specific tasks.

Python lists are probably the first data type that most Python beginners start learning in a sequence category, and along with this, their functions and methods. The `reverse()` function is one of the functions of Python Lists used to reverse the elements from the list.

To get the same work done, Python has a function named `reversed()`, which is also used to reverse the sequence of data.

In this article, we will deep dive into both functions and understand their fundamentals and usage along with examples, and then at the end of this article, we’ll point out the differences between them.

# List reverse() function

As we discussed earlier, the `reverse()` function from Python List is used to reverse the elements in a List. This function does what its name depicts itself.

The list `reverse()` function operates on the original list. It reverses the list and then returns it. If we try to print the original list, we’ll get the reversed list because the original list no longer exists, and the reversed list is the original.

Output

We tried to print the original list, but due to the use of the reverse() function, our list got modified, and we got the reversed list.

## Syntax – reverse()

list.reverse()

Parameter

There are no parameters, and neither it takes any arguments.

Return Value

It does not return any value but reverses the elements from the list.

## Usage – reverse()

We have two lists in the following example, the first list has collections of integers, and the second contains multiple data types.

We used the reverse() function on both lists and got the output as we expected it should be.

We put the reverse() function to practical use in the following example when we check whether a specified value is a palindrome.

The value “malayalam” is a palindrome because it will remain the same whether we reverse it or not. That’s why we got the output “It’s a Palindrome” because the original and reversed lists are the same.

What will happen if we try to reverse a string? In the above example, we specified the string but converted it into the list before performing the reverse operation.

We’ll get an error if we use the `reverse()` function on a string.

The error says that the string has no attribute reverse. We cannot use the reverse function on the string data type or any other data type. The `reverse()` function can only be used for the list data type.

# Python reversed() function

Python `reversed()` function takes a sequence and returns the reversed iterator object. It is the same as the `iter()` method but in reverse order. We can access the reversed objects either by iterating them or using the `next()` function.

We used the `next()` function to access the iterator objects.

## Syntax – reversed()

reversed(sequence)

Parameter

• sequence – Any sequence object that is iterable.

Return Value

Returns the reversed iterator object of the given sequence.

## Usage – reversed()

We have already seen the example where we performed the reverse operation on the Python list using the `reversed()` function. This time we’ll try to reverse the Python string.

We used the `for` loop to iterate over the reversed sequence and then printed it in a single line.

Similarly, we can use any sequence object that is iterable. ListTuple, and Dictionary objects are iterable and can be reversed. The Set objects can also be iterated but they are not reversible.

The output will be a reversed tuple and keys of a dictionary.

# What’s the difference?

We’ve seen the definition and usage of both functions and are now able to differentiate between them. The primary work of both functions is to reverse the objects of the given sequence. But there are some key differences between the `reverse()` and `reversed()` functions which are listed below.

# Conclusion

This article covered the fundamentals and usage of the `reverse()` and `reversed()` functions and we coded some examples along with them. Through this article, we meant to dive into the working and usage of the functions and of course, point out the key differences between them.

Let’s review what we’ve learned:

• Usage of list `reverse()` function
• Usage of the Python `reversed()` function
• Key differences between both functions

That’s all for now

Keep Coding✌✌