0
Python - Why is the output {True: 'maybe'}?
a = {True: 'yes', 1: 'no', 1.0: 'maybe'} print(a) # outputs {True: 'maybe'}
3 Answers
+ 13
Dictionary keys must be unique.
True == 1 == 1.0
Interpret it this way:
a = { }
a [True] = 'yes'
a [ 1 ] = 'no'
a [1.0] = 'maybe'
a[1] = ".." and a[1.0] = ".." just override the value since the key already exists.
https://code.sololearn.com/cUdWK5zB5Xyv/?ref=app
+ 5
More than one entry per key not allowed.
Which means no duplicate key is allowed.
When duplicate keys encountered during assignment, the last assignment wins.
If your keys can only take values ââlike (True, 1, 1.0), then try this:
https://code.sololearn.com/cGLkOjDniF2D/?ref=app
+ 2
it's something like:
x, x, x = 'yes', 'no', 'maybe'
print(x) # will output 'maybe'