-1

I have a list of 200 elements, i want to loop through the list in increments of 10.

  1. Result 1 = list[0:10]
  2. Result 2 = list[11:21]
  3. Result 3 = list[22:32]
  4. Result 20 = list[190:199]

Whats the most pythonic way to do this?

mikelowry
  • 1,307
  • 4
  • 21
  • 43
  • 1
    Your example list has 201 elements. – Alex Reynolds Oct 08 '20 at 22:11
  • 1
    I strongly suspect that using 20 individual variables is not going to be a good long-term solution for you. Also, note that your sample lists are 11 elements long. – Prune Oct 08 '20 at 22:21
  • @Prune using 10 at a time is a great long-term solution for what im trying, why do you suspect otherwise? Maybe I'm missing some preconceived notion. – mikelowry Oct 08 '20 at 22:28
  • Please read my comment again; I didn't question grabbing 10 elements at a time. – Prune Oct 08 '20 at 22:31
  • @prune still not understadning, please clarify "20 individual variables", are you talking about the partitioning of the list into 20 sublists? If thats the case, my original comment stands. – mikelowry Oct 08 '20 at 23:13
  • 2
    What @Prune is suggesting is that for a collection of similar things, you probably don't want to assign them to individually named variables (like `Result11`), but you want a structure to hold them, like a list, such as`Results = []`, and then you don't need 20 literal names, but can do `Results[11]`, `Results[5]`, etc. – tom10 Oct 09 '20 at 00:02

1 Answers1

3

You have to split your list into chunks:

chunks = [xlist[x:x+10] for x in range(0, len(xlist), 10)]

for chunk in chunks:
    print(len (chunk)) # prints out  10
obayhan
  • 1,636
  • 18
  • 35