Counting and incrementing duplicate values

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

  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.
1 Like

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

@nico.dehnuk ,

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

it is a starting point…

KR

Andreas

2 Likes

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.

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
6 Likes

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

1 Like

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

You guys are great

Pay it forward :slight_smile:

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.

2 Likes

Hi,

another solution with list.count()

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