# Group Repeated Sequential Items in List

**URL:** <https://forum.dynamobim.com/t/group-repeated-sequential-items-in-list/62329>\
**Category:** Lists-Logic\
**Created:** [March 30, 2021, 11:40am UTC](https://forum.dynamobim.com/t/group-repeated-sequential-items-in-list/62329 "2021-03-30T11:40:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Keith\_Wilkinson1](https://avatars.discourse-cdn.com/v4/letter/k/5f8ce5/32.png) [@Keith\_Wilkinson1](https://forum.dynamobim.com/u/Keith_Wilkinson1)\
**Post date:** [March 30, 2021, 11:40am UTC](https://forum.dynamobim.com/t/group-repeated-sequential-items-in-list/62329/1 "2021-03-30T11:40:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![\_Vijay](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/_vijay/32/91307_2.png) [@\_Vijay](https://forum.dynamobim.com/u/_Vijay)\
**Post date:** [March 30, 2021, 1:45pm UTC](https://forum.dynamobim.com/t/group-repeated-sequential-items-in-list/62329/2 "2021-03-30T13:45:14Z")

</div>

See if that helps. Better show some images.

> [@Need help with a list sorting](https://forum.dynamobim.com/t/need-help-with-a-list-sorting/62178/8):
>

---

<div class="post-metadata">

**Author:** ![Keith\_Wilkinson1](https://avatars.discourse-cdn.com/v4/letter/k/5f8ce5/32.png) [@Keith\_Wilkinson1](https://forum.dynamobim.com/u/Keith_Wilkinson1)\
**Post date:** [March 30, 2021, 2:36pm UTC](https://forum.dynamobim.com/t/group-repeated-sequential-items-in-list/62329/3 "2021-03-30T14:36:03Z")

</div>

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?

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/1/31f5878a5189cd20fc92cf2b9330c22ac9e85371.png)

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

---

<div class="post-metadata">

**Author:** ![AmolShah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/amolshah/32/103244_2.png) [@AmolShah](https://forum.dynamobim.com/u/AmolShah)\
**Post date:** [March 30, 2021, 2:59pm UTC](https://forum.dynamobim.com/t/group-repeated-sequential-items-in-list/62329/4 "2021-03-30T14:59:35Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/8/c/8c509d99176e0ddd46496c7f71e23abc228bb03c.png)

```
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:

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/2/42ce5e32952e82103eff276dc408dcea06893cf4.png)

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

---

<div class="post-metadata">

**Author:** ![AmolShah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/amolshah/32/103244_2.png) [@AmolShah](https://forum.dynamobim.com/u/AmolShah)\
**Post date:** [March 30, 2021, 9:58pm UTC](https://forum.dynamobim.com/t/group-repeated-sequential-items-in-list/62329/5 "2021-03-30T21:58:24Z")

</div>

> [@Keith\_Wilkinson1](#):
>
> Once I have the number of consecutive items would then need to drop the duplicates from the original list…

Python can help you directly get that as well!

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/9/29d18c570c9ccff0ef2c580ca2b4beeb38d0dea8.png)

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

```

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

 ![SplitList_2021-03-30_02-57-43](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/d/0daeee52ab928d6a331c210442a13294973248db.png)  
I took this [post](https://forum.dynamobim.com/t/split-the-list-by-boolean/62093/13) as a base but I’m sure there would be better node solutions out there.  
[SplitList.dyn](https://forum.dynamobim.com/uploads/short-url/jfcxkk2FIp50l7yZE6RqeR2HJRC.dyn) (32.5 KB)

---

<div class="post-metadata">

**Author:** ![honeyjain619](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/honeyjain619/32/121215_2.png) [@honeyjain619](https://forum.dynamobim.com/u/honeyjain619)\
**Post date:** [March 31, 2021, 7:22am UTC](https://forum.dynamobim.com/t/group-repeated-sequential-items-in-list/62329/6 "2021-03-31T07:22:01Z")

</div>

[list value.dyn](https://forum.dynamobim.com/uploads/short-url/bBZ13J95L8xNzisTj0Tn24hEM0Y.dyn) (10.0 KB)

 ![1](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/9/49139970a4aa400f624c07dedf3a0e6e11122579.jpeg)

I thing its easy,

---

<div class="post-metadata">

**Author:** ![Keith\_Wilkinson1](https://avatars.discourse-cdn.com/v4/letter/k/5f8ce5/32.png) [@Keith\_Wilkinson1](https://forum.dynamobim.com/u/Keith_Wilkinson1)\
**Post date:** [March 31, 2021, 8:58am UTC](https://forum.dynamobim.com/t/group-repeated-sequential-items-in-list/62329/7 "2021-03-31T08:58:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Keith\_Wilkinson1](https://avatars.discourse-cdn.com/v4/letter/k/5f8ce5/32.png) [@Keith\_Wilkinson1](https://forum.dynamobim.com/u/Keith_Wilkinson1)\
**Post date:** [March 31, 2021, 8:59am UTC](https://forum.dynamobim.com/t/group-repeated-sequential-items-in-list/62329/8 "2021-03-31T08:59:52Z")

</div>

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.
