How can I reverse a list in Python?

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

    >>> L = [0,10,20,40]
    >>> L[::-1]
    [40, 20, 10, 0]
    

    Extended slice syntax is explained well in the Python What's new Entry for release 2.3.5

    By special request in a comment this is the most current slice documentation.

提交回复
热议问题