Splitting sublists by true values loop through lists

Hello wizards, I am back for more of your awesome guidance. I am attempting to split lists of lists by true values. I have python script which works for a single list, but am lost when i have multiple lists how to get the python to work. Attached image shows the AllIndicesOf values for true values. I want to ultimately chop the lists at all these values to create new sublists. I think i need a loop or a zip function possibly but i have never used one. I am hoping i made sense and that someone can shed some insight to make this work.

Why Python? And does it have to be Python?

One liner to get indexes - assumes a list of lists with true/false

indexes = [[i for i, v in enumerate(lst) if v] for lst in IN[0]]

Now to slice the lists on the index

def slicer(values, indexes):
    indexes.sort()
    return filter(None, [values[s:e] for s, e in zip([0]+indexes, indexes+[len(values)])])
    
    
values = IN[1]
indexes = [[i for i, v in enumerate(lst) if v] for lst in IN[0]]

OUT = [slicer(v, i) for v, i in zip(values, indexes)]

It doesn’t have to be python if you have a solution without it.

Good to know.
I allways like to provide non Python :snake: solutions (IF possible) for people who
don’t know Python (like me :see_no_evil:)

Is @Mike.Buttery ‘example’ what you were after?

Python makes this really easy, but you can still do it with nodes. You just need to use the true indices to define start/stop sequences and be mindful of the list levels.

2 Likes

Both @Mike.Buttery and @Nick_Boyts solutions work great. I guess it just depends on whether you like python or prefer use of nodes. Thank you much gentlemen!

I was experimenting and i came up with something like this :point_down:.

But i would need some sort a CumulativeSum but one which would substract.

You can essentially do that really easily by just creating a list of “shifted indices” by adding 0 to the beginning and subtracting.


However, flattening the list means you no longer respect the original sublists. Not sure if that’s what’s intended or not.

2 Likes

Fair point. And thanks for the example how to subtract a value with the previous.
Learned something again today (that’s i also why i post my brain farts sometimes :blush:).

If you have Python chops consider a yielding function, I use that for breaking down lists into chunks in this video and use this technique often when dealing with things like finding continuous periods of solar access.

1 Like

Same logic as the python with OOTB nodes - I would have a list.clean node at the end, however that is not removing empty lists in Dynamo Sandbox 2.19 set preserveIndices to false

1 Like