 Chop list at index

Hi everyone, I am having some trouble by finding the list count at index. I want to chop a list using the index number or find the length of similar values in the list.

I have the index value of all the “True” values but to chop the list I need the list length. For instance the list length should read 2, 29, 2, 7, 2 etc
Can anyone help?

So if I understand this correctly, you want to chop (or essentially group) consecutive values with the same boolean. i.e. Collect all the Trues until you hit a False, then collect all the Falses until you hit a True. This would be much simpler in python, but it’s still probably doable with nodes.

I am not too well versed in Python, but you have it correct. This grouping will form the basis of many angular and length calculations in the script so its quite crutial…

See if this serves your purpose

bln2 = List.Sublists(bln1,0..1,1);
bln3 = List.AllTrue(bln2<1>) ? false:List.LastItem(bln2<1>) == true ? true:List.FirstItem(bln2<1>) == true ? true:false;
ind0 = List.AllIndicesOf(bln3,true)+1;
ind1 = List.Flatten([0,ind0,List.Count(bln1)],-1);
ind2 = List.RestOfItems(ind1)-List.DropItems(ind1,-1);
lst1 = List.Chop(lst,ind2);
2 Likes

Is there a way to only group the false values?

You could try to pass the list of bools through the same codeblock (same input at lst and bln) Get the first item (or common item) of each sub list and use that as the mask in FilterByBoolMask

Below is an alternate version of a similar process, but with the content of the code block converted to a function

{
bln2 = List.Sublists(bln1,0..1,1);
bln3 = List.AllTrue(bln2<1>) ? false
:List.LastItem(bln2<1>) == true ? true
:List.FirstItem(bln2<1>) == true ? true
:false;
ind0 = List.AllIndicesOf(bln3,true)+1;
ind1 = List.Flatten([0,ind0,List.Count(bln1)],-1);
ind2 = List.RestOfItems(ind1)-List.DropItems(ind1,-1);
return List.Chop(lst,ind2);
};

1 Like

Here’s a python version that doesn’t necessarily have to use booleans. It just splits a list of items based on groupings of paired values.

You could easily modify it to look specifically for False or any other value by changing the query to the target value and removing the output section of the else statement.

Code
items = IN
vals = IN
out = []
sub = []
items.append(None)
vals.append(None)
prev = vals

for item,val in zip(items,vals):
if val == prev:
sub.append(item)
prev = val
else:
out.append(sub)
sub = []
sub.append(item)
prev = val

OUT = out
2 Likes

OK WOW. My head is a bit spinning right now trying to understand how to do this.