Wednesday 28 December 2016

python - What does list[x::y] do?










I've been reading over some sample code lately and I've read quite a few websites but I just can't seem to get the query right to give me an answer I'm looking for. If anyone could help me out I'd appreciate it.


Answer



It slices




x[startAt:endBefore:skip]


if you use skip = 2, every other element the list beginning at startAt and ending at endBefore will be selected. [Remember: indices live BETWEEN list elements]



To see this, enter



x = range(100)



at the Python prompt. Then try these things



x[::2]
x[::3]
x[10:40:6]


and see what happens.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...