Multiple keys per value

What type are the values?

dict = {'k1':MyClass(1), 'k2':MyClass(1)}

will give duplicate value objects, but

v1 = MyClass(1)
dict = {'k1':v1, 'k2':v1}

results in both keys referring to the same actual object.

In the original question, your values are strings: even though you’re declaring the same string twice, I think they’ll be interned to the same object in that case


NB. if you’re not sure whether you’ve ended up with duplicates, you can find out like so:

if dict['k1'] is dict['k2']:
    print("good: k1 and k2 refer to the same instance")
else:
    print("bad: k1 and k2 refer to different instances")

(is check thanks to J.F.Sebastian, replacing id())

Leave a Comment