2

Is there a good way to print n elements of a list, for example 10, after that the next 10 numbers?

    liste = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
    for i in range(0,len(liste),10):
        for j in range(10):
        print(liste[j])

This just prints the numbers from 1-10 and begins at 1 again.

I want something like this:

1,2,3,4,5,6,7,8,9,10
11,12,13,14,15,16,17,18,19,20

bassically cut the list into parts of n elements the same size.

Emanuel
  • 69
  • 1
  • 10

2 Answers2

3

Use python list slicing:

your_list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

for i in range(0, len(your_list), 10):
    chunk = your_list[i:i+10]
    print(','.join(list(map(str,chunk))))

Outputs:

1,2,3,4,5,6,7,8,9,10
11,12,13,14,15,16,17,18,19,20

Related links:

How do you split a list into evenly sized chunks?

Rithin Chalumuri
  • 1,739
  • 7
  • 19
0

The value of j runs from 0 to 9 on every iteration.

Try print(liste[i*10+j]).

goodvibration
  • 5,980
  • 4
  • 28
  • 61