How do ::-1 works as a list operator for reversing it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do ::-1 works as a list operator for reversing it?

Didn't get the sense of ::

13th Oct 2020, 8:24 PM
Diga17
Diga17 - avatar
5 Answers
+ 1
List[start:end:step] so here ::-1 means the step is - 1 which will go backwards from the end to the start and give you the reverse
13th Oct 2020, 8:32 PM
Ruba Kh
Ruba Kh - avatar
+ 3
This used to print all elements of a list from the last element to the first element
13th Oct 2020, 8:29 PM
Muhammad Galhoum
Muhammad Galhoum - avatar
+ 2
In a list, when you try to access an element, you would use the square brackets, in python, they have a lot of fonctionality, they can get an index "[5]", get you a range "[5:10]" and get a range with steps "[5:11:2]". Those option can be optional, for exemple, "[:10]" will give get you all element from start to 10. so this is why you only see "::-1" writen this way, it means from start to end with a negative step wich will reverse the order.
13th Oct 2020, 8:38 PM
Apollo-Roboto
Apollo-Roboto - avatar
+ 2
In slice [A:B:C] you add C to A until A = B. If the new slice would never be achieveable, it gives an empty slice.
13th Oct 2020, 8:50 PM
Seb TheS
Seb TheS - avatar
+ 1
Wow, that took 7 minutes to get an answer! So kind and quick♥
13th Oct 2020, 8:34 PM
Diga17
Diga17 - avatar