# Duplicates in Lists

Hello, in short I am trying to keep a list in tact, but replace all duplicate values, besides the first duplicate, with a string like “X” or something similar.

For example, I am trying to take a list like this:
“1, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 9, 10”

Into a single list like this:
“1, 2, 3, X, 4, 5, 6, 7, X, X, 8, 9, 10”

Thanks!

I would use Python for this. You could make an empty list, pass it through and if statement to check if each value is in the new list, then if not append it and if so append X instead.

I’m on my mobile but it’s be like this…

oldlist = IN[0]

newlist =

for i in oldlist:
if i in newlist:
newlist.append(“X”)
else:
newlist.append(i)

OUT = newlist

1 Like

Hi @markb43 , welcome to the Dynamo community!

As @GavinCrump mentioned, I would go with python in this one. But here’s 2 more possible solutions that you can implement:

Or

``````lst;
replace = "X";
[Imperative]
{
for(i in GetKeys(lst))
{
count = List.Count(lst[i]);
if (count>1)
{
for (j in (1..count-1))
lst[i][j] = replace;
}
}
return = List.Flatten(lst);

};
``````
5 Likes

Hi @markb43,
Welcome to the Dynamo community.
You can do something like this as well:

Replace OK with your string and you should be good to go.

1 Like

You could refer to the code below and do this with nodes too (if you prefer)

``````idx = 0..List.Count(lst)-1;
unq = List.IndexOf(lst,List.UniqueItems(lst));
dup = List.SetDifference(idx,unq);
rpl = List.Contains(dup,idx<1>) ? "X" : lst;
``````

EDIT: Line 3 is actually unnecessary.

`List.Contains(List.IndexOf(n,List.UniqueItems(n)),(0..List.Count(n)-1)<1>) ? n : "X";`

7 Likes

This one works great, Thanks!

1 Like