-3

question

my_list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,...,50]

answer

listOne = [0,1,2,....,9
listTwo = [10,11,12,...,19]
listThree = [20,21,22,...,29]
listFour = [30,31,32,...,39]
listFive = [40,41,42,...,49]
listSix = [50,51,52,...,59]

answer

If we do not know the number to show in my list how to split list

2 Answers2

0
def SplitList(given_list, chunk_size):
    return [given_list[offs:offs+chunk_size] for offs in range(0, len(given_list), chunk_size)]

Use this function to pass the list:

chunk_list = SplitList(my_list, 10)
for lst in chunk_list:
    print(lst)
Pankaj
  • 931
  • 8
  • 15
0

You can use mlist[i : i+10] to split every 10 element in a group

#populate list
mlist = []
for i in range (51):
   mlist.append(i)
print("##########INPUT##########")
print(mlist)
new = []
for i in range(0, len(mlist), 10):
 new.append(mlist[i : i+10])
print("##########OUTPUT##########")
print("Total Group: "+str(len(new)))
for i in range(len(new)):
   print(new[i])

The output will be like this
##########INPUT##########
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
##########OUTPUT##########
Total Group: 6
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
[50]
WenJuan
  • 624
  • 1
  • 8
  • 21