Skip to content
Advertisement

How to concatenate a list of sublists equally chunked

I have a list that contains sublists of two different types of items (for instance, cats and dogs). I also have a function chunk(list, n_sublists) that splits the list into n_sublists of equal size.

Then, i’d like to create a final list that merges the chunked lists from each type. An example:

JavaScript

I hope the example makes it clear. However, i can provide more explanation if needed.

Thanks in advance.

Advertisement

Answer

You can do a loop on zip:

JavaScript

Output:

JavaScript

Update: in general, use reduce

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement