Given a list:
- l1:
['a', 'b', 'c', 'a', 'a', 'b']
- output:
['a', 'b', 'c', 'a_1', 'a_2', 'b_1' ]
I created the following code to get the output. It's messyyy..
for index in range(len(l1)):
counter = 1
list_of_duplicates_for_item = [dup_index for dup_index, item in enumerate(l1) if item == l1[index] and l1.count(l1[index]) > 1]
for dup_index in list_of_duplicates_for_item[1:]:
l1[dup_index] = l1[dup_index] + '_' + str(counter)
counter = counter + 1
Is there a more pythonic way of doing this? I couldn't find anything on the web.