Friday, September 13, 2019

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

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...