Adding x number of items to each page : Python

Given a value of x, I need to display x items on each page.

For eg: I have a list of 10 items. Given x = 3 I need to display 3 items on page1, 3 on page2, 3 on page3 and 1 on page4 value of x can be odd or even.

Stuck on how to break or start a new list for the second iteration.

2 answers

  • answered 2018-09-25 01:13 Jim Todd

    Something like below will be of help:

    a=20 #Actual value holder
    x=3  #the 'x' you need
    rem_value=a%x
    quot=int(a/x)
    
    for y in range(quot):
      print(str(x)+" in page "+str(y+1))
      print('\n') 
    
    if(rem_value)>0:
      print(str(rem_value)+" in page "+str(quot+1))
    

    *> Output: (when 20 in input)

    3 in page 1
    3 in page 2
    3 in page 3
    3 in page 4
    3 in page 5
    3 in page 6
    2 in page 7*

  • answered 2018-09-25 06:03 vash_the_stampede

    I'm going to put the same effort into explaining, that you put into attempting

    x = 3
    l = [i for i in range(10)]
    l1 = [l[i:i + x] for i in range(0, len(l), x)]