Home > front end >  Does python dictionary have duplicate keys?
Does python dictionary have duplicate keys?

Time:02-03

I am trying to create a python dictiory with dupliate key as follows.

x = {"a" :1,"b":2,"z": 4, "c":90, "z":0, "k":None}
print(x["z"])

when i am calling key "z" value it is printing 0 why it is not printing 4? does it valid to have duplicat keys in dictionary?

CodePudding user response:

Keys within a dictionary in python are unique, therefore you cannot have duplicate keys. The reason you are getting "0" is that when you define "z" the second time in your dictionary, this key/value pair is overwriting your original pair.

CodePudding user response:

Python dictionary does not supports multiple key. You can store multiple values on one key using a list. Something like:

dictionary = {
'a' : 1,
'z': [0, 4]
}
  •  Tags:  
  • Related