Replace consecutive similar numbers in a list

is there any way to replace one of the consecutive similar numbers in a list with Null or 0 and not delete them?
for example: (1, 2, 2, 3) to (1, Null, 2, 3)

Might the value 2 occur again later on in the list?

yeah. but not consecutive. it might be like: 1, 2, 2, 3, 2, 4, 5, 5… and i need: 1, null, 2, 3, 2, 4, null, 5

I ask because this seems to work, but it won’t if the numbers happen again: NullifyDuplicates.gh (23.3 KB)

that is the problem. the numbers might show up again

This seems to work.


ReplaceConsecutiveDuplicates.gh (9.8 KB)

1 Like

Perfect!! Thanks a lot! I should have asked earlier:)

Actually, Michael’s solution does not work (see items 2 & 14, and 6 & 13, they are the same). No offense!
I found a solution that works with random lists, although it’s probably not the simplest way. It would also need some adjusments if you want it to work on more than one list at a time.


Cull duplicates.gh (13.5 KB)

Actually, Michael’s solution does not work (see items 2 & 14, and 6 & 10

It does work, They are not consecutive. Read the question, the goal is to replace consecutive duplicates, not duplicates in general.

Oops, sorry! You’re right, my bad…

No Worries, yours is good for another situation.

@ala_hzd and if you want it to read like your post describes where the nulls come first then the one remaining of the conseccutive duplicates you can just reverse the start list, then reverse it back at the end.


ReplaceConsecutiveDuplicatesRev.gh (12.7 KB)

Hi Michael,
Thank you for this approach. Maybe you can help me with the below question, as I think your approach here could be the base. I’ve done a rather long and complicated solution, and I’m sure there is a simple one I’ve missed.

I have a list of lists with random numbers (the number of items in each list can vary). I’m looking for a way to avoid duplicate consecutive numbers. When there is a duplicate number I wish to replace one of them with a different number from a list of numbers. In this case a list of 4 numbers: 0,1,2,3.

I’ll be grateful if you could share your approach for solving this issue.

All the best!
Yoav

That’s elementary via code. Notify if you need a C# demo (in replace action and when mode is for an internal demo … a rnd num (6666 to 6668) is used).


Hi Peter,
Thank you for your reply! (I’m a big fan :)) You are right! that is elementary via code, I will get there eventually… I see you haven’t posted your solution? In the meanwhile just had a nice chat with ChatGPT which helped work out a nice solution via Python. What do you think?

import random

def main(x, y):
a =
previous_number = None
for i in range(len(x)):
number = x[i]
if number == previous_number:
if i < len(x) - 1 and x[i + 1] != number:
a.append(x[i + 1])
previous_number = x[i + 1]
else:
new_number = random.choice(y)
while new_number == number or (i < len(x) - 1 and new_number == x[i + 1]):
new_number = random.choice(y)
a.append(new_number)
previous_number = new_number
else:
a.append(number)
previous_number = number
return a

a = main(x, y)

Jesus !!! Man that thing just promotes free Lobotomy for all (the final target): Avoid AT ANY COST.

Lists_RemoveConcequiveDupes_V1.gh (122.3 KB)

BTW: I hate P like my sins (a lot).

Tips: Learn C# - the good old way - ASAP. Ride a proper Ducati (ditto). Sell the computer (ditto).

1 Like

Thank you Peter, really appreciate your work (and approach!)

Added a 4th option and PlanC: can you - one day - modify this and replace the main C# ?. And where is the bug on PlanB ? (see num 100)

Lists_ManageConsequiveDupes_V1A.gh (121.5 KB)