'Replacing duplicate elements inside 2D List from other List

I have this code of replacing duplicate elements in 2D list but only first index are changing duplicates and the other index elements keep shuffling. Thankyouuuuu have a great day!!

import random
ch = ['a','b','c','d','e','f',]
my_tuple_1 = [['a', 'b', 'b', 'd'],['a', 'c', 'c', 'd']]
my_set = set()
my_result = [tuple(ele if ele not in my_set and not my_set.add(ele) else random.choice(ch)for ele 
in y ) for y in my_tuple_1]
print("The tuple after replacing the values is: ")
print(my_result)
#OUTPUT - > [('a', 'b', 'e', 'd'), ('d', 'c', 'd', 'f')]
#EXTECTED OUTPUT - > [('a', 'b', 'e', 'd'), ('a', 'c', (get random from ch), 'd')]


Solution 1:[1]

my_result = []
for y in my_tuple_1:
  my_set = set()
  my_tuple = ()
  for ele in y:
    if ele not in my_set:
      my_tuple += (ele,)
      my_set.add(ele) 
    else:
      random_ch = random.choice(list(set(ch) - set(my_tuple)))
      my_tuple += (random_ch,)
      my_set.add(random_ch)

  my_result.append(my_tuple)

Output:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1