Group Repeated Sequential Items in List

Hi,

Looking for some pointers on where to start with this as I’m at a bit of a loss.

I have a variable number of lists with a variable number of items in each. In these lists some elements repeat sequentially that I would like to ‘Group’. For instance, the list B, A, B, C, C, C, A, C, A, A would become B, A, B, 3C, A, C, 2A.

Ideally, I’d like to do it with nodes as my Python journey has stalled yet again but even a starting point would be great!

Any help gratefully received.

Cheers

K.

See if that helps. Better show some images.

No, unfortunately not… this might explain it better, I essentially need to count the number of consecutive items in a list… when the item value changes the count restarts. Does that make sense?

Once I have the number of consecutive items would then need to drop the duplicates from the original list…

@Keith_Wilkinson1 I know it’s Python but see if this works for you.

from itertools import groupby
grouped = [(len(list(groups)),keyValue) for keyValue,groups in groupby(IN[0])]
OUT = [str(count)+str(key) if count>1 else str(key) for count,key in grouped]

And if you need only the count then try this:

from itertools import groupby
OUT = [len(list(groups)) for keyValue,groups in groupby(IN[0])]

Python can help you directly get that as well!

from itertools import groupby
OUT = [keyValue for keyValue,groups in groupby(IN[0])]

Or you can do something using the OOTB nodes like this.


I took this post as a base but I’m sure there would be better node solutions out there.
SplitList.dyn (32.5 KB)

list value.dyn (10.0 KB)

I thing its easy,

Ah, this is genius and does exactly what i need I need to go and unpick it a little but some new stuff in here for me to learn.

I’ll take a look at the Python script as well but this works for me right now, many thanks for taking the time to help, much appreciated.

Thanks for taking the time to look at this but unfortunately what you have doesn’t quite work for me as it’s just totaling the different unique values - I need to preserve the original list format and just group values where they duplicate. @AmolShah solution hits the mark though so all good from my end.