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}]

```
Thanks to @jizhihaoSAMA I have managed to find a solution to my problem with a small edit

```
machines = [{"Program Name": "Bulldozer", "Level": 3}, {"Program Name": "Robot", "Level": 1}, {"Program Name": "Bulldozer", "Level": 4}]
        tmp = defaultdict(int)
        for i in machines:
            name = i["Program Name"].strip(f' ({tmp[i["Program Name"]]})')
            i["Program Name"] = f'{name} ({tmp[i["Program Name"]]})' if tmp[name] else i["Program Name"]
            tmp[name] += 1

```

This disables problems with spaces in the program name or any complexed names.