PythonLists and tuples
list. reverse()
Reverses the order of the items in a list in place.
- Returns
None- Changes the original
- Yes
Example
#Python
levels = [1, 2, 3, 4]
levels.reverse()
print(levels)
original = ["a", "b", "c"]
print(original[::-1])
print(original)Resultsaved result, you can check it
[4, 3, 2, 1]
['c', 'b', 'a']
['a', 'b', 'c']
Definition and usage
#The reverse() method reverses the list: the first item becomes the last. It changes the list in place and returns None.
It does not sort, it only reverses the current order. For descending order through sorting, use sort(reverse=True).
If you need a reversed copy without changing the original, use the slice my_list[::-1] or the reversed() function.
Syntax
#Syntax
list.reverse()Good practices
#- The slice
[::-1]creates a reversed copy and does not change the original. - To loop from the end without copying, use
reversed(my_list).
Related entries
#Spotted a mistake or missing an example? Write to us.