0

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?

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119

2 Answers2

0

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.

0

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]
}