Question: I have a list of dicts with the same structure

```
[{"Program Name": "Bulldozer", "Level": 3}, {"Program Name": "Robot", "Level": 1}, {"Program Name": "Bulldozer", "Level": 4}]

```

What I want is duplicate keys of "Program Name" that have the same value (ex: "Bulldozer" appearing 2x) to be renamed as "Bulldozer (1)", "Bulldozer (2)" and so on.

Answer: 
An Efficient way is to use `defaultdict` to count the `"Program Name"`, the time complexity
is `O(n)`:

```
from collections import defaultdict

l = [{"Program Name": "Bulldozer", "Level": 3}, {"Program Name": "Robot", "Level": 1},
     {"Program Name": "Bulldozer", "Level": 4}, {"Program Name": "Bulldozer", "Level": 4}, {"Program Name": "Robot", "Level": 1}]

tmp = defaultdict(int)
for i in l:
    i["Program Name"] = f'{i["Program Name"]} ({tmp[i["Program Name"]]})' if tmp[i["Program Name"]] else i["Program Name"]
    tmp[i["Program Name"].split()[0]] += 1

print(l)

```

Result:

```
[{'Program Name': 'Bulldozer', 'Level': 3}, {'Program Name': 'Robot', 'Level': 1}, {'Program Name': 'Bulldozer (1)', 'Level': 4}, {'Program Name': 'Bulldozer (2)', 'Level': 4}, {'Program Name': 'Robot (1)', 'Level': 1}]

```
You can try this too:

```
data = [
    {"Program Name": "Bulldozer", "Level": 3},
    {"Program Name": "Robot", "Level": 1},
    {"Program Name": "Bulldozer", "Level": 4},
    {"Program Name": "Rozer", "Level": 3},
    {"Program Name": "Robot", "Level": 1},
    {"Program Name": "Rozer", "Level": 3},
    {"Program Name": "Bulldozer", "Level": 3},
    {"Program Name": "Robot", "Level": 1},
    {"Program Name": "Bulldozer", "Level": 4},
    {"Program Name": "Rozer", "Level": 3},
    {"Program Name": "Robot", "Level": 1},
    {"Program Name": "Rozer", "Level": 3}
]

```

`Approach: 01`

```
import pandas as pd

c = pd.DataFrame(data)
c['group_code'] = c.groupby(['Program Name']).cumcount() + 1
c['Program Name'] = ["{0} ({1})".format(x, y) for (x, y) in c[[
    'Program Name', 'group_code']].values]
output = c[['Program Name', 'Level']].to_dict(orient='records')
print(output)

```

`Approach: 02`

```
temp = {}
for item in data:
    temp.update(
        {
            item['Program Name']: temp[item['Program Name']] + 1 if temp.get(item['Program Name']) else 1
        }
    )
    item['Program Name'] = item['Program Name'] + ' (' + str(temp[item['Program Name']]) + ')'

print(data)

```

output:

```
[
    {"Program Name": "Bulldozer (1)", "Level": 3},
    {"Program Name": "Robot (1)", "Level": 1},
    {"Program Name": "Bulldozer (2)", "Level": 4},
    {"Program Name": "Rozer (1)", "Level": 3},
    {"Program Name": "Robot (2)", "Level": 1},
    {"Program Name": "Rozer (2)", "Level": 3},
    {"Program Name": "Bulldozer (3)", "Level": 3},
    {"Program Name": "Robot (3)", "Level": 1},
    {"Program Name": "Bulldozer (4)", "Level": 4},
    {"Program Name": "Rozer (3)", "Level": 3},
    {"Program Name": "Robot (4)", "Level": 1},
    {"Program Name": "Rozer (4)", "Level": 3}
]

```

I would recommend you to use `pandas(approach 01)` if you have huge amount of data.