SoFunction
Updated on 2025-03-02

Analysis of the difference between reversed and reverse in python

reverse()

reverse() is a built-in method unique to python lists. This method cannot be used in dictionaries, tuples, and strings. It is used to invert and reverse elements in lists.

lista = [1, 2, 3, 4]
()
print(lista)

#[4, 3, 2, 1]

reversed()

reversed() is a built-in function in Python, but it returns an inverted iterator.
And reversed() isA method that comes with python, to be precise, it should be a class;
That is to say, after the reversed() function, the returned iterator after the sequence value is reversed, soNeed to pass traversal, or List, or next() and other methods, get the value after effect.
The following are explained through several cases:
1. Reversal of the list:

>>> bb = [1,3,5,7]          
>>> print(list(reversed(bb)))

#[7, 5, 3, 1]

2. Inversion of tuples:

>>> aa = (1, 2, 3)           
>>> print(tuple(reversed(aa)))   
     
#(3, 2, 1)

3. Inversion of strings

>>> aa = 'asbdamfgh'          
>>> ''.join(reversed(aa)) Not applicablestr(reversed(aa))
'hgfmadbsa'

This is the end of this article about the difference between reversed and reverse in python. For more related content on the difference between reversed and reverse, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!