# Counting and incrementing duplicate values

**URL:** <https://forum.dynamobim.com/t/counting-and-incrementing-duplicate-values/91906>\
**Category:** Developers\
**Tags:** revit, dynamo\
**Created:** [July 18, 2023, 2:26pm UTC](https://forum.dynamobim.com/t/counting-and-incrementing-duplicate-values/91906 "2023-07-18T14:26:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nico.dehnuk](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@nico.dehnuk](https://forum.dynamobim.com/u/nico.dehnuk)\
**Post date:** [July 18, 2023, 2:26pm UTC](https://forum.dynamobim.com/t/counting-and-incrementing-duplicate-values/91906/1 "2023-07-18T14:26:47Z")

</div>

Hi all,  
I want to increment duplicate values in a list.  
As an example:  
If item 1 and item 6 have the same value (xxx) the result should look like this:

Item 1 = xxx\_1  
Item 6 = xxx\_2

Does anyone have an idea?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [July 18, 2023, 2:40pm UTC](https://forum.dynamobim.com/t/counting-and-incrementing-duplicate-values/91906/2 "2023-07-18T14:40:37Z")

</div>

1. Group all your items by value. This will give you sublists of both unique and duplicated items.
2. Filter out all the sublists that only have a single item so you’re left with only the duplicates.
3. Count the number of items (`n`) in each duplicate sublist and create sequences from 1 to `n`.
4. Append the new count (`n`) to each item in the list of duplicates.

---

<div class="post-metadata">

**Author:** ![nico.dehnuk](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@nico.dehnuk](https://forum.dynamobim.com/u/nico.dehnuk)\
**Post date:** [July 19, 2023, 6:50am UTC](https://forum.dynamobim.com/t/counting-and-incrementing-duplicate-values/91906/3 "2023-07-19T06:50:24Z")

</div>

Hi Nick  
thanks for your reply. Unfortunately, I wasn’t precise enough. The items are parameter values of elements (doors). I would like to return the supplemented values to the elements as new parameter values. That’s why I’m not allowed to rearrange the list. Can you think of a solution to this? Thanks again

---

<div class="post-metadata">

**Author:** ![Draxl\_Andreas](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/draxl_andreas/32/145744_2.png) [@Draxl\_Andreas](https://forum.dynamobim.com/u/Draxl_Andreas)\
**Post date:** [July 19, 2023, 7:32am UTC](https://forum.dynamobim.com/t/counting-and-incrementing-duplicate-values/91906/5 "2023-07-19T07:32:40Z")

</div>

@nico.dehnuk ,

Object.Identity is very strong regarding detecting such “twins” 😉

it is a starting point…

 ![grafik](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/4/341df4afa07fe02c0b69eeaf87a3350a6af3e2cc.png)

KR

Andreas

---

<div class="post-metadata">

**Author:** ![pyXam](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/pyxam/32/163009_2.png) [@pyXam](https://forum.dynamobim.com/u/pyXam)\
**Post date:** [July 19, 2023, 8:37am UTC](https://forum.dynamobim.com/t/counting-and-incrementing-duplicate-values/91906/6 "2023-07-19T08:37:46Z")

</div>

I’m sorry. i could only make this work for a flattened list.  
My brain exploded when i tried to make it work for lists of lists. (Probably due to a few beverages) so hopefully this works for you.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/a/fa0e62786ee335656f4bea5850d16fd88177c126.png)

```auto
def find_duplicates(input_list):
    count_dict = {}
    output_list = []

    for item in input_list:
        if item in count_dict:
            count_dict[item] += 1
        else:
            count_dict[item] = 0

    for item in input_list:
        if count_dict[item] > 0:
            suffix = count_dict[item]
            count_dict[item] -= 1
            output_list.append(f"{item}_{suffix}")
        else:
            output_list.append(item)

    return output_list

# Test Input
input_list = IN[0]

# Find duplicates and get unique items
duplicates = set()
unique_items = []
for item in input_list:
    if item in duplicates:
        continue
    if item in input_list[input_list.index(item)+1:]:
        duplicates.add(item)
        unique_items.append(item)
    else:
        unique_items.append(item)

# Adjust suffixes for duplicates
suffix_dict = {}
for item in unique_items:
    if item in duplicates:
        suffix_dict[item] = 1
    else:
        suffix_dict[item] = 0

output_list = []
for item in input_list:
    if item in duplicates:
        output_list.append(f"{item}_{suffix_dict[item]}")
        suffix_dict[item] += 1
    else:
        output_list.append(item)

# Set the output with the same order as input
OUT = output_list

```

---

<div class="post-metadata">

**Author:** ![nico.dehnuk](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@nico.dehnuk](https://forum.dynamobim.com/u/nico.dehnuk)\
**Post date:** [July 19, 2023, 9:30am UTC](https://forum.dynamobim.com/t/counting-and-incrementing-duplicate-values/91906/7 "2023-07-19T09:30:15Z")

</div>

This works absolutely great. Thank you for your support. You have saved me an incredible amount of time and work

---

<div class="post-metadata">

**Author:** ![nico.dehnuk](https://avatars.discourse-cdn.com/v4/letter/n/90ced4/32.png) [@nico.dehnuk](https://forum.dynamobim.com/u/nico.dehnuk)\
**Post date:** [July 19, 2023, 9:32am UTC](https://forum.dynamobim.com/t/counting-and-incrementing-duplicate-values/91906/8 "2023-07-19T09:32:14Z")

</div>

Many thanks to the community. I didn’t expect so many helpful answers in such a short time.

You guys are great

---

<div class="post-metadata">

**Author:** ![pyXam](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/pyxam/32/163009_2.png) [@pyXam](https://forum.dynamobim.com/u/pyXam)\
**Post date:** [July 19, 2023, 9:35am UTC](https://forum.dynamobim.com/t/counting-and-incrementing-duplicate-values/91906/9 "2023-07-19T09:35:02Z")

</div>

Pay it forward 🙂

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [July 19, 2023, 1:34pm UTC](https://forum.dynamobim.com/t/counting-and-incrementing-duplicate-values/91906/10 "2023-07-19T13:34:29Z")

</div>

> [@nico.dehnuk](#):
>
> That’s why I’m not allowed to rearrange the list. Can you think of a solution to this?

Just as an FYI, this is still possible. You just have to get the original indices of the ordered list and group them based on value as well. Then you can put your grouped items back in the appropriate order with their respective indices.

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [July 22, 2023, 8:12am UTC](https://forum.dynamobim.com/t/counting-and-incrementing-duplicate-values/91906/11 "2023-07-22T08:12:29Z")

</div>

Hi,

another solution with list.count()

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/e/de7d7b77e629c5ecee49dff92ed01fda1602fd71.png)

```auto
import sys
lstA = IN[0]
OUT = [f"{val}_{lstA[:idx +1].count(val)}" for idx, val in enumerate(lstA)]

```
