[PYTHON] List reverse operation

Frequently used list reverse order operation

>>> list = [1,2,3]
>>> reversed(list) # => <listreverseiterator object>
>>> list.reverse() # =>Destructive operation
>>> list
[3,2,1]
>>> list = [1,2,3]
>>> list[::-1]     # =>The simplest and most powerful
[3,2,1]

When applying a function to a list in reverse order

>>> list = [1,2,3]
>>> def inc(n):
...     return n+1
...
>>> #Redundant but easy to understand
>>> for i in reversed(list):
...     print inc(i)
...
4
3
2
>>> #Be careful because it is a destructive operation
>>> list.reverse()
>>> for i in list:
...     print inc(i)
...
4
3
2
>>> list = [1,2,3]
>>> map(inc, list[::-1]) # =>Concise and powerful
[4, 3, 2]

Recommended Posts

List reverse operation
List operation memo writing
Operation of filter (None, list)
Algorithm Gymnastics 24 Reverse a Linked List
Python basic operation 1st: List comprehension notation
List comprehension
linked list
Bit operation
[Introduction to Udemy Python3 + Application] 17. List operation
Join list
List comprehension
Basic operation list of Python3 list, tuple, dictionary, set