Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list reversion: [::-1]?

I can't find any information on [::-1]. In the wikibooks python tutorial, there is a section about non-continous lists, but there's no information on parameters < 0. Effects are clear, but how do you explain it?

Example Usage:

>>> foo = [1, 2, 3]
>>> foo[::-1]
[3, 2, 1]
like image 942
f4lco Avatar asked Dec 17 '22 02:12

f4lco


1 Answers

The syntax is as follows:

foo[start:end:step] # begin with 'start' and proceed by step until you reach 'end'.

So foo[::-1] means entire list with step=-1, so actually reversing the list.

See this answer for detailed explanation.

like image 193
Mariusz Jamro Avatar answered Jan 10 '23 13:01

Mariusz Jamro