-7
l = [1,2,3,4,5,6,7,8,9,10]

I have a list and I need to print 5 elements per line to get:

1 2 3 4 5
6 7 8 9 10

I tried everything but I'm stuck, help!

Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
  • 4
    _I tried everything but.._ post your efforts – B001ᛦ May 30 '18 at 15:16
  • **@Joaquin**, you can use `%` operator to check the index of 5th, 10th, 15th,...items and based on that you can go to new line otherwise continue printing items in the same line. – hygull May 30 '18 at 15:33

1 Answers1

0

@Joaquin, try below code:

http://rextester.com/BLN7722

l = [1,2,3,4,5,6,7,8,9,10]

for index, item in enumerate(l):
    if (index + 1) % 5 == 0:
        print(item)
    else:
        print(item, end=" ")

"""
1 2 3 4 5
6 7 8 9 10
"""
hygull
  • 8,464
  • 2
  • 43
  • 52