-2

I have array of attachmentList and

if I do

len(attachmentList) 

the result is 199930; and I would like to send 999 elements each time to api_request(attachment) function

so pseudo code will be like this

count=0
for (i=0,i<len(attachmentList),i++)
     count++
     if count=999:
       api_request(attachmentList[i-999:i])
       count=0

What is the way to write for loop or there is another solution for that.

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
draford
  • 243
  • 1
  • 3
  • 15

3 Answers3

1

Use the grouper recipe:

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

And then:

for chunk in grouper(attachmentList, 1000):
    api_request(chunk)
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
1

You can loop in chunks of 999:

for i in range(0, len(attachmentList), 999):
    api_request(attachmentList[i:i+999])
sashkello
  • 17,306
  • 24
  • 81
  • 109
0

You can use range(...) function as:

previous = 0
for i in range(999,len(attachmentList),999):
    api_request(attachmentList[previous:i]
    previous = i
Ruben Bermudez
  • 2,293
  • 14
  • 22