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)
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']}
I see there are three Eli
s, 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']}
votes_dictionary={}
for i in votes:
try:
votes_dictionary[i].append(i)
except KeyError:
votes_dictionary[i] = [i]
print(votes_dictionary)
myDictionary = {'johny': [], 'Eli': [], 'Johny': [], 'Jane': [], 'john': [], 'Ally': []}
votes = ['johny', 'Eli', 'Eli', 'Jane', 'Ally', 'Johny', 'john', 'Eli']
for x in votes:
myDictionary[x].append(x)
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.