rename duplicates in list python 1

rename duplicates in list python

[v + str(mylist[:i].count(v) + 1) if mylist.count(v) > 1 else v for i, v in enumerate(mylist)]

Here is what the above code is Doing:
1. enumerate(mylist) returns a list of tuples, where each tuple is (index, value)
2. for each tuple, we check if the value is in the list more than once. If it is, we add the index to the value.
3. if the value is not in the list more than once, we just return the value.

Similar Posts