-2

can someone help me out.

I need a loop that returns 3 results from a list for each loop

Example:

lst = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

for x in lst:
    print(x[0])
    print(x[1])
    print(x[2])

should return

1,2,3

4,5,6

7,8,9

10,11,12

13,14,15

16

im using flask

{% for i in getdataasset %}
    {{getdataasset[i]}}
{% endfor %}
pyrazs
  • 7
  • 3

1 Answers1

2

Use slices

lst = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

for i in range(0,len(lst),3):
    print(*lst[i:i+3])
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16
Алексей Р
  • 7,507
  • 2
  • 7
  • 18