1

So I have a 'for' statement that appends items onto the end of one list, but I only want a set amount of these items to be added to the list, before starting a new one. I thought the easiest way to do this would be to take the end list, and for every 7 items (the amount I need), create a new list (doesn't have to be in a variable) EG:

list1 = [1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6,7]

I want:

[1,2,3,4,5,6,7]    
[1,2,3,4,5,6,7]    
[1,2,3,4,5,6,7]    

to be derived from this, anyone got any ideas?

Constantly Confused
  • 595
  • 4
  • 10
  • 24

1 Answers1

0

Slicing is the best way to seperate the list into smaller list.

>>> list1[:7], list1[7:14], list1[15:]
[1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5, 6, 7] [2, 3, 4, 5, 6, 7]
Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24