Reputation: 43
I have a big array of 10573 elements and I want to group them into chunks but skipping two elements after each chunk.
I have the code to divide the list into chunks:
chunk_size= 109
for i in range(0, len(ints), chunk_size):
chunk = ints[i:i+chunk_size]
But how do I skip or delete two elements from the big list iteratively, i.e., after attaining each chunk of size 109?
Is there a way to do that?
Upvotes: 0
Views: 299
Reputation: 782693
Add 2 to the chunk size when using it in the iteration.
chunk_size= 109
for i in range(0, len(ints), chunk_size+2):
chunk = ints[i:i+chunk_size]
Upvotes: 3
Reputation: 11209
Use modular arithmetics :
blocksize = chuncksize + 2
newints = [i for i in ints if i%blocksize < chuncksize]
The other way is to loop backward:
blocksize = chuncksize + 2
for i in range(len(ints), 0, -blocksize)
ints.pop(i+chuncksize)
ints.pop(i+chuncksize+1)
Note: Did not test.
Upvotes: 0