Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Flip a List

Python Flip a List

Document Object Reference Manual Python3 Examples

Define a list and flip it.

For example:


翻转前 : list = [10, 11, 12, 13, 14, 15]
翻转后 : [15, 14, 13, 12, 11, 10]

Example 1

def

Reverse

(

lst

)

:

   

return

[

ele

for

ele

in

reversed

(

lst

)

]

     

lst

=

[

10

,

11

,

12

,

13

,

14

,

15

]

print

(

Reverse

(

lst

)

)

The output of the above example is:


[15, 14, 13, 12, 11, 10]

Example 2

def

Reverse

(

lst

)

:

    lst.

reverse

(

)

   

return

lst

     

lst

=

[

10

,

11

,

12

,

13

,

14

,

15

]

print

(

Reverse

(

lst

)

)

The output of the above example is:


[15, 14, 13, 12, 11, 10]

Example 3

def

Reverse

(

lst

)

:

    new_lst

=

lst

[

::-

1

]

   

return

new_lst

     

lst

=

[

10

,

11

,

12

,

13

,

14

,

15

]

print

(

Reverse

(

lst

)

)

The output of the above example is:


[15, 14, 13, 12, 11, 10]

Document Object Reference Manual Python3 Examples