- A+

Category：Languages

I am trying to get *m* values while stepping through every *n* elements of an array. For example, for *m* = 2 and *n* = 5, and given

`a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] `

I want to retrieve

`b = [1, 2, 6, 7] `

Is there a way to do this using slicing? I can do this using a nested list comprehension, but I was wondering if there was a way to do this using the indices only. For reference, the list comprehension way is:

` b = [k for j in [a[i:i+2] for i in range(0,len(a),5)] for k in j] `

I agree with wim that you can't do it with just slicing. But you can do it with just one list comprehension:

`>>> [x for i,x in enumerate(a) if i%n < m] [1, 2, 6, 7] `