I am trying to merge my dictionary with same key but it is a little bit different because one of the dictionary is containing a list. I am trying to get an output which will also give me a list back, if there is more than one value in the merged dictionary, like :
{'cartoon':['tom','jerry'],'film':['rose','jack','marry'],'serial':'little boy'}
I am using python 3.7. Is it possible to run the code without using defaultdict? I was also trying to write the print statement like:
print("{'cartoon':"+list(d1(key(value))+list(d2[key]+",'film':"+list(d1(key(value))+list(d2[key]+",'serial':"+list(d1(key(value))+list(d2(key(value))+"}"
but it did not work, so I wrote the following code:
from collections import defaultdict
d1 = {'cartoon': 'tom', 'film': 'rose'}
d2 = {'cartoon': 'jerry', 'film':['jack','marry'], 'serial' : 'little boy'}
new = defaultdict (list)
for char in (d1,d2):
for key, value in char.iteritems():
new[key].append(value)
print (new)
I am getting an error saying "AttributError: 'dict' object has no attribute 'iteritems'.
Can anyone suggest it how can I edit this? Thanks in advance.