Skip to content
Advertisement

Add a character to a specific duplicate string

I’m trying to add a character to a specific duplicate string, like in the following situation:

nomes_comp = ['Fulano A', 'Beltrano B', 'Fulano A']

So I have duplicate items on my list; I would like to add another character in one of the duplicate items, hoping to get the following output:

nomes_comp = ['Fulano A', 'Beltrano B', 'Fulano A1']

I’m trying this way, but it’s not working:

JavaScript

Advertisement

Answer

Use a dictionary (or a collections.defaultdict) to keep track of how many occurrences you’ve found.

JavaScript

After you do this, the original nomes_comp is modified to:

JavaScript
Advertisement