How can I reverse a list in Python?

后端 未结 30 2612
既然无缘
既然无缘 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:21

    Strictly speaking, the question is not how to return a list in reverse but rather how to reverse a list with an example list name array.

    To reverse a list named "array" use array.reverse().

    The incredibly useful slice method as described can also be used to reverse a list in place by defining the list as a sliced modification of itself using array = array[::-1].

提交回复
热议问题