1

I wanted to append to respective keys in a dictionary in the list of string

myDictionary = {'johny': [], 'Eli': [], 'Johny': [], 'Jane': [], 'john': [], 'Ally': []} votes = ['johny', 'Eli', 'Eli', 'Jane', 'Ally', 'Johny', 'john', 'Eli'] outPut={'johny': ['johny'], 'Eli': ['Eli','Eli'], 'Johny': ['Johny'], 'Jane': ['Jane'], 'john': ['john'], 'Ally': ['Ally']} 

I tried to do like this but appends the whole list in each key

votes_dictionary={} votes_dictionary=votes_dictionary.fromkeys(votes,[]) for i in votes: print(i.lower()) votes_dictionary[i].append(i) print(votes_dictionary) 
3
  • no need to check for duplicate keys Commented Mar 10, 2020 at 4:58
  • 1
    Why is 'Eli':['Eli,'Eli'] when 'Eli' appeared 3 times in votes list shouldn't it be 'Eli': ['Eli','Eli','Eli']? Commented Mar 10, 2020 at 4:58
  • Yes that's right its my mistake Commented Mar 10, 2020 at 5:01

5 Answers 5

5

You can use defaultdict with list as default value and then iterate through votes and append it:

from collections import defaultdict votes = ['johny', 'Eli', 'Eli', 'Jane', 'Ally', 'Johny', 'john', 'Eli'] votes_dictionary = defaultdict(list) for vote in votes: votes_dictionary[vote].append(vote) # votes_dictionary will be an instance of defaultdict # to convert it to dict, just call dict print(dict(votes_dictionary)) # outpout {'johny': ['johny'], 'Eli': ['Eli', 'Eli', 'Eli'], 'Jane': ['Jane'], 'Ally': ['Ally'], 'Johny': ['Johny'], 'john': ['john']} 
Sign up to request clarification or add additional context in comments.

Comments

0
votes_dictionary={} for i in votes: try: votes_dictionary[i].append(i) except KeyError: votes_dictionary[i] = [i] print(votes_dictionary) 

Comments

0

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

Comments

0
myDictionary = {'johny': [], 'Eli': [], 'Johny': [], 'Jane': [], 'john': [], 'Ally': []} votes = ['johny', 'Eli', 'Eli', 'Jane', 'Ally', 'Johny', 'john', 'Eli'] for x in votes: myDictionary[x].append(x) 

Comments

0
votes_dictionary = {} votes_dictionary = votes_dictionary.fromkeys(votes) for i in votes: if not votes_dictionary[i]: votes_dictionary[i] = [] votes_dictionary[i].append(i) print(votes_dictionary) 

A lot of answers have been posted, if you really want to use the fromkeys() method, you could do something like this

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.