Home > Software engineering >  I tried updating dictionary with existing dictionaries using update method but its showing error : T
I tried updating dictionary with existing dictionaries using update method but its showing error : T

Time:06-27

I HAVE TO UPDATE D1 AND D2 IN D3

d1={"adam smith":"A","judy paxton":"B "}
d2={"mary louis":"A","patrik white":"C"}
d3={}

THIS IS MY CODE

for item in (d1,d2):
    d3.update({d1:d2})
print(d3)

And it giving error unhashable type: 'dict'

CodePudding user response:

for item in (d1,d2):
    d3.update({d1:d2})
print(d3)

It gives you a TypeError, when you are trying to use d1 as a key, because keys can only be of hashable types. You can't use dict as a key in another dict, unless it is a custom class dict where the __hash__ method is defined.

As it was said, this will do:

for item in (d1,d2):
    d3.update(item)
print(d3)

CodePudding user response:

Just do:

for item in (d1,d2):
    d3.update(item)
print(d3)
  • Related