0
data = [
    
    ['customers/2309565764/assets/34830517871', 
    'customers/2309565764/assets/34827154141', 
    'customers/2309565764/assets/34856605170', 
    'customers/2309565764/assets/34830515300'
    ],
     
    ['customers/2309565764/assets/34830515303', 
    'customers/2309565764/assets/34830473309', 
    'customers/2309565764/assets/34830508136', 
    'customers/2309565764/assets/34830515336'], 

    ['customers/2309565764/assets/20029085042', 
    'customers/2309565764/assets/20033811553', 
    'customers/2309565764/assets/20065471524', 
    'customers/2309565764/assets/20029089104'], 

    ['customers/2309565764/assets/20033813263', 
    'customers/2309565764/assets/20065476771', 
    'customers/2309565764/assets/20029091753', 
    'customers/2309565764/assets/20029091369'],

    ['customers/2309565764/assets/11111', 
    'customers/2309565764/assets/22222', 
    'customers/2309565764/assets/33333', 
    ],

    ['customers/2309565764/assets/20033813263', 
    'customers/2309565764/assets/20065476771', 
    'customers/2309565764/assets/20029091753', 
    'customers/2309565764/assets/20029091369'],

    ['customers/2309565764/assets/20033813263', 
    'customers/2309565764/assets/20065476771', 
    'customers/2309565764/assets/20029091753', 
    'customers/2309565764/assets/20029091369'],

    ['customers/2309565764/assets/20033813263', 
    'customers/2309565764/assets/20065476771', 
    'customers/2309565764/assets/20029091753', 
    'customers/2309565764/assets/20029091369'],

    ['customers/2309565764/assets/20033813263', 
    'customers/2309565764/assets/20065476771', 
    'customers/2309565764/assets/20029091753', 
    'customers/2309565764/assets/20029091369'],

    ['customers/2309565764/assets/20033813263', 
    'customers/2309565764/assets/20065476771', 
    'customers/2309565764/assets/20029091753', 
    'customers/2309565764/assets/20029091369'],

    ['customers/2309565764/assets/20033813263', 
    'customers/2309565764/assets/20065476771', 
    'customers/2309565764/assets/20029091753', 
    'customers/2309565764/assets/20029091369'],
    ]

what I want to get out of it

The same list of lists, but each of the internal lists should have no more than 20 items. It will end up looking like this

[
    ['customers/2309565764/assets/34830517871', 
    'customers/2309565764/assets/34827154141', 
    'customers/2309565764/assets/34856605170', 
    ....
    20th element],
    
    ['customers/2309565764/assets/34830517871', 
    'customers/2309565764/assets/34827154141', 
    'customers/2309565764/assets/34856605170', 
    ....
    20th element],
    
    ['customers/2309565764/assets/34830517871', 
    'customers/2309565764/assets/34827154141', 
    'customers/2309565764/assets/34856605170', 
    ....
    last element],
    
    ]

Using this code

result = []
transitional_data = []
for i in data:
    if len(transitional_data) + len(i) <= 20:
        for j in i:
            transitional_data.append(j)
    else:
        result.append(transitional_data)
        transitional_data = []
print(result)    

I end up with only one nested list instead of 3 (two of which will have 20 elements and one with 4 residual elements)

[
    ['customers/2309565764/assets/34830517871', 
    'customers/2309565764/assets/34827154141', 
    'customers/2309565764/assets/34856605170', 
    ....
    20th element],  
] 

Important condition on nested lists in data - the elements of a nested list cannot be separated, i.e. for example the first two elements of

['customers/2309565764/assets/34830517871', 
'customers/2309565764/assets/34827154141', 
'customers/2309565764/assets/34856605170', 
'customers/2309565764/assets/34830515300'
],

can't get into the first sublist, and the other two can't get into the other one. There can be <= 4 items in the original nested lists.

upd I updated input data, now 5th nested list consist 3 elements. And if I run

flat_list = [item for sublist in data for item in sublist]
partitioned_list = [flat_list[i:i + 20] for i in range(0, len(flat_list), 20)] 

Result data consist 3 nested list - this is what I need to get. But first element of 6th nested list is in the first resulted sub-list and the other 2 are in the second result sub-list

Jekson
  • 2,892
  • 8
  • 44
  • 79
  • can you post a function to generate the orignal list of list? because current raw data is unnecessarily too long. – Lei Yang Mar 23 '22 at 13:24
  • no sorry, its incoming data – Jekson Mar 23 '22 at 13:30
  • 2
    i know that. but you don't have to post the real data here. minimal is expected. say, `20` in your question can be `3`. – Lei Yang Mar 23 '22 at 13:33
  • 3
    Just flatten the list and chunk. Both of those questions have been asked before. – trincot Mar 23 '22 at 13:34
  • Lei Yang the data is always different, I gave one option. I.e. there may be no nested lists, or there may be one or a hundred – Jekson Mar 23 '22 at 13:39
  • @trinkot These answers do not take into account the important condition that I described in the question: the elements of a nested list cannot be separated. – Jekson Mar 23 '22 at 13:46
  • If that condition is important, please give an example input where the trivial solution won't work. Also, your example output is impossible from the example input. Please use shorter sequential data elements so people can make sense of what you're after. – Bill the Lizard Mar 23 '22 at 13:54
  • @ScottMcC I think you voted to close the question without getting to the core of the issue. – Jekson Mar 23 '22 at 14:06
  • @Bill the Lizard I updated my question – Jekson Mar 23 '22 at 14:08
  • 2
    `ll = []; acc = []; for subl in original_list: {if len(acc)+len(subl) <= 20: acc.extend(subl) else: {ll.append(acc); acc = list(subl)}}; if acc: ll.append(acc); return ll` – Stef Mar 23 '22 at 15:24

1 Answers1

0

Here's a version that takes the special condition into consideration. I simplified the values in the data list to make it easier to read the input and output.

data = [
    ['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']
]


def combine(original_list, maxlen=20):
    outer_list = []
    accum = []

    for sublist in original_list:
        # if the next set of values will fit, extend the sublist
        if len(accum) + len(sublist) <= maxlen:
            accum.extend(sublist)
        else:
            # otherwise start a new sublist
            outer_list.append(accum)
            accum = list(sublist)

    # pick up any values that are left over
    if accum:
        outer_list.append(accum)

    return outer_list

new_list = combine(data)
print(new_list)

Based on the idea in Stef's comment, this accumulates values in a list until the maximum length is reached, but without splitting the sublists in the original input.

Output:

[['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']]
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880