I see there are three Elis, so typically it would look like this:
output = {} for name in votes: output.setdefault(name, []) output[name].append(name) print(output)
Output:
{'johny': ['johny'], 'Eli': ['Eli', 'Eli', 'Eli'], 'Jane': ['Jane'], 'Ally': ['Ally'], 'Johny': ['Johny'], 'john': ['john']}
Or,
import copy output = copy.deepcopy(mydictionary) for name in votes: output[name].append(name) print(output):
output:
{'johny': ['johny'], 'Eli': ['Eli', 'Eli', 'Eli'], 'Johny': ['Johny'], 'Jane': ['Jane'], 'john': ['john'], 'Ally': ['Ally']}
Now, if you want to limit to two even if there are three elements:
output = {} for name in votes: # to maintain the order, you can iterate over `mydictionary` output[name] = [name]*min(2, votes.count(name)) print(output)
output:
{'johny': ['johny'], 'Eli': ['Eli', 'Eli'], 'Jane': ['Jane'], 'Ally': ['Ally'], 'Johny': ['Johny'], 'john': ['john']}
Another fun way to achieve two occurrences of Eli would be, itertools.groupby:
>>> from itertools import groupby >>> {key: [*group] for key, group in groupby(reversed(votes))} {'Eli': ['Eli', 'Eli'], 'john': ['john'], 'Johny': ['Johny'], 'Ally': ['Ally'], 'Jane': ['Jane'], 'johny': ['johny']}
'Eli':['Eli,'Eli']when'Eli'appeared 3 times in votes list shouldn't it be'Eli': ['Eli','Eli','Eli']?