0

Python - Why is the output {True: 'maybe'}?

a = {True: 'yes', 1: 'no', 1.0: 'maybe'} print(a) # outputs {True: 'maybe'}

6th Nov 2020, 8:38 AM
Solus
Solus - avatar
3 ответов
+ 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
6th Nov 2020, 8:52 AM
Kevin ★
+ 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
6th Nov 2020, 11:19 AM
Vitaly Sokol
Vitaly Sokol - avatar
+ 2
it's something like: x, x, x = 'yes', 'no', 'maybe' print(x) # will output 'maybe'
6th Nov 2020, 9:34 AM
Yurii Ostapenko
Yurii Ostapenko - avatar