Home python flip list

# flip list

Author

Date

Category

Do not tell why an example from Documentation does not work?

``````a = [66.25, 333, 333, 1, 1234.5]
Print (A.REverse (), END = '\ N \ N \ n')
Print (A)
``````

I have a result on the screen:

none

[1234.5, 1, 333, 333, 66.25]

Python 3.3 version

Because the Reverse method turns over the list, but does not return anything.

``````& gt; & gt; & gt; a = [1, 2, 3]
& gt; & gt; & gt; A.
[1, 2, 3]
& gt; & gt; & gt; a.reverse ()
& gt; & gt; & gt; A.
[3, 2, 1]
``````

If you want the function to return an inverted list without changing it – use the function Reversed (be careful, the function will return the iterator!)

``````& gt; & gt; & gt; a = [1, 2, 3]
& gt; & gt; & gt; List (REVERSED (A))
[3, 2, 1]
& gt; & gt; & gt; A.
[1, 2, 3]
``````

You can copy the list of the entire `:: `and organize in the reverse order – to the Ave. so:

``````for i in a [:: - 1]: print i, "\ n"
``````

Print is an implicit concatenation of rows over a comma – you can and \ N \ N soak, on version 3 it should also be.

In principle, the best answer has already been given – the Reversed method (in contrast reverse ()):

``````for i in reversed (a): Print i
``````

Also, if I am so verbal, you may need a type variation (stack – First in Last Out):

``````a = [66.25]
a.append (333)
a.append (333)
a.pop ()
333.
a.pop ()
333.
a.pop ()
66.25
``````

And where is the example of the documentation does not work? Everything works. Reverse turned out. Reverse! = Sort.

``````buff = [1,2,3,4,5,6,7,8,9,10]
buff0 = buff [:: - 1] # turn the list of width order
Print (Buff0) # [10, 9, 8, 7, 5, 4, 3, 2, 1]
``````

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.