0

I have this nested list:

summed = [[2, 3, 534, 56, 67, 676, 3423, 234, 34, 75], [546, 678, 23, 57, 7867, 54236, 325, 6546, 587, 114]]

I want to create a new nested list so that it will split the two previous lists into two more. The new list will look like:

new_list = [[[2, 3, 534, 56, 67], [676, 3423, 234, 34, 75]], [[546, 678, 23, 57, 7867, 54236, 325, 6546, 587, 114]]]. Every five numbers of each list will be a new list.

I want to create it with for or while loop. I have done this so far.

i = 0
while i<len(summed):
    new_list = []
    j = 0
    while (j<len(summed[i])):
        new_list.append(summed[i][j])
        j+=5
    new_list2.append(new_list)
    i+=1
print new_list
  • Slices would probably be easiest (`new_list = [[l[:len(l)//2], l[len(l)//2:]] for l in summed]`), but the linked questions and answers make more use of loops, as you request. – TigerhawkT3 Apr 01 '17 at 09:48
  • @TigerhawkT3 i still cant figure out how to do it in my example :/ – Thanos S Apr 01 '17 at 12:45
  • @TigerhawkT3 because what happens if i want to split not to half, to create for example 5 lists out of 1 – Thanos S Apr 01 '17 at 14:23
  • Then you use the more general strategies outlined in the linked questions and answers. – TigerhawkT3 Apr 01 '17 at 20:17

0 Answers0