I'm attempting to iterate through a list and if the list item equals a dictionary key, append the list item to the dictionary.
mylist = [1, 2, 3]
mydict = dict.fromkeys(mylist, [])
for item in mylist:
for key in mydict:
if key == item:
mydict[key].append(item)
print(mydict)
Output:
{1: [1, 2, 3], 2: [1, 2, 3], 3: [1, 2, 3]}
Required output:
{1: [1], 2: [2], 3: [3]}
Much thanks!