1

So I am working on a how to be able to print out 50 values from a list each time, meaning if a list contains a length of 120 values, it should print out first 50, then the second 50 and then the rest 20 in that case but the 120 would be a value of X meaning it could be changing all the time:

so what I have created so far is basically just a simple list

listTest = [1233,12412,12412,34653,78556,23523,1231231,345353,4574574,234524523,24124,34534534,4576445,23523523]

for x in listTest:
    print(x)


data = {
    "recipients": #List of first 50 then it should loop and take the second 50, 
                  # then loop again through this request and so on,
    "originator": "testing",
}

response = requests.post("https://test.com/messages", json=data)

which I just made a simple loop through the list but I have no idea on how to make it work that it should print first 50 values in a list, then the second 50 in a list, then the third 50 in a list etc etc until there is no more values in the list.

So it would be like etc:

print
    > list of first [50 values]
    > list of second [50 values]
    > list of third [50 values]
    > list of fourth [24 values]

How can I be able to do that?

Thrillofit86
  • 599
  • 2
  • 7
  • 20
  • 2
    Please I would like to know what was the reason of downvoting beacuse that is a way I can learn how to write better next time than just downvoting me :( Would apprecaite it! – Thrillofit86 Jan 26 '20 at 19:05
  • I imagine it's because this is a question that's come up many times before... however, it's not always easy to find unless you search for the word "chunk". – Jon Clements Jan 26 '20 at 19:11
  • @JonClements Ahh it could be what I was looking for indeed. The word Chunk but I guess I also got the help from here which is nice! – Thrillofit86 Jan 26 '20 at 19:12
  • 1
    I didn't downvote but i'd imagine its because you havent shown any effort in solving the issue yourself – Sayse Jan 26 '20 at 19:33

3 Answers3

2

Maybe not the best way but you can do it:

count=0
list=[] 
for x in listTest:
    count+=1
    list.append(x)
    if count%50==0:
        print(list)
        list=[]

#for last values
print(list) 
Renaud
  • 2,709
  • 2
  • 9
  • 24
1

you can do this way, using list slicing

l = [i for i in range(432)]
for i in range(0,len(l), 50):
     print(a[i:i+50])
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
1

you can use range build-in function and slicing:

for index in range(0, len(list_test), 50):
    print(list_test[index: index + 50])
kederrac
  • 16,819
  • 6
  • 32
  • 55