0

Can wE usEd 2 oR mOre saMe kEys iN a diCtioNary

yes or not plz tell me, if yes then how?

18th Feb 2017, 4:47 PM
R K
R K - avatar
5 Answers
+ 6
nO, keYs iN dicTionAriEs haVe to bE uniQue.
18th Feb 2017, 7:12 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 3
Yes and No. Lets say we created a dictionary in this manner: dict = {'key1': 'value1', 'key2': 'value2', 'key1': 'value3'} Yes this will work just fine. However, No you will not have 2 different values for key1 and you will not have 3 key-value pairs in your dictionary. The second call to key1 will overwrite the first value of key1 ending up with a dictionary like so: {'key1': 'value3', 'key2': 'value2'}
18th Feb 2017, 5:17 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
If you declare two or more key/values pair with same key, last declaration will overwrite previous: d = { "key": "value", True: "why not boolean", "key": 42, 1: "you can also integer", "1": "different from previous" } ... will result as: d == { "key": 42, True: "you can also integer", "1": "different from previous" } As True and 1 are equals in Python, assignement to integer key 1 overwrite boolean key True, while string key "1" does'nt interfere... And dicts are unordered list, so you cannot predict order, but order of declaration is important for declaration in case of overwritting, and for the textual representation ( with first boolean key assignement, the key keep the representation of "True", even we can access it with True or 1... while with first integer declaration, the representation of the key will remain "1" along the key life )
19th Feb 2017, 8:32 AM
visph
visph - avatar
0
Why would you want that? Keys have to be unique
18th Feb 2017, 4:53 PM
Zilvinas Steckevicius
Zilvinas Steckevicius - avatar
0
ya I know bt my question is can I used?
18th Feb 2017, 4:54 PM
R K
R K - avatar