Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is :: (double colon) in Python when subscripting sequences?

I know that I can use something like string[3:4] to get a substring in Python, but what does the 3 mean in somesequence[::3]?

like image 364
Aillyn Avatar asked Aug 10 '10 20:08

Aillyn


People also ask

What does double :: mean in Python?

Answer: The double colon is a special case in Python's extended slicing feature. The extended slicing notation string[start:stop:step] uses three arguments start , stop , and step to carve out a subsequence. It accesses every step -th element between indices start (included) and stop (excluded).

What does :: In Python means?

Artturi Jalli. In Python, [::-1] means reversing a string, list, or any iterable with an ordering. For example: hello = "Hello world"

What is :: In slicing?

Consider a python list, In-order to access a range of elements in a list, you need to slice a list. One way to do this is to use the simple slicing operator i.e. colon(:) With this operator, one can specify where to start the slicing, where to end, and specify the step.

What does [: :] do in Python?

It selects all but the last element of a sequence.


2 Answers

it means 'nothing for the first argument, nothing for the second, and jump by three'. It gets every third item of the sequence sliced. Extended slices is what you want. New in Python 2.3

like image 191
Adriano Varoli Piazza Avatar answered Sep 18 '22 15:09

Adriano Varoli Piazza


Python sequence slice addresses can be written as a[start:end:step] and any of start, stop or end can be dropped. a[::3] is every third element of the sequence.

like image 40
deinst Avatar answered Sep 21 '22 15:09

deinst