Home > Software design >  Problem with adding a value to a key in a dictionary on python. I am not sure what is not working he
Problem with adding a value to a key in a dictionary on python. I am not sure what is not working he

Time:07-30

counts = dict()
names = ['David', 'Daniel', 'Michelle', 'Daniel', 'Ben', 'Ben', 'Daniel']
for name in names:
    if name not in names:
        counts[name] = 1
    else:
        counts[name] = counts[name]   1
print(counts)

CodePudding user response:

small error, should be if name not in counts:

counts = dict() names = ['David', 'Daniel', 'Michelle', 'Daniel', 'Ben', 'Ben', 'Daniel'] for name in names: if name not in counts: counts[name] = 1 else: counts[name] = counts[name] 1

  • Related