How can I reverse a list in Python?

后端 未结 30 2574
既然无缘
既然无缘 2020-11-21 22:32

How can I do the following in Python?

array = [0, 10, 20, 40]
for (i = array.length() - 1; i >= 0; i--)

I need to have the elements of a

30条回答
  •  无人共我
    2020-11-21 23:32

    There are 3 methods to get the reversed list:

    1. Slicing Method 1: reversed_array = array[-1::-1]

    2. Slicing Method 2: reversed_array2 = array[::-1]

    3. Using the builtin function: reversed_array = array.reverse()

    The third function actually reversed the list object in place. That means no copy of pristine data is maintained. This is a good approach if you don't want to maintain the old version. But doesn't seem to be a solution if you do want the pristine and reversed version.

提交回复
热议问题