-1

What is output of code:

print({True:'yes', 1:'no', 1.0:'maybe'}) 

Please explain it with answer

Avoid
  • 343
  • 2
  • 9
  • 20

1 Answers1

0

In Python dictionary, you cannot have duplicate keys. What is happening in your case is that all the keys in your dictionary evaluate to the boolean value True.

True, 1.0 and 1 all are equal to -> True

So what is happening is that first the value with key True gets a value 'yes' then its value is overridden by 'no' and finally by 'maybe'

Hence you have a resulting dictionary with only one key True with value 'maybe'

So basically this is happening:

True:'yes' == True:'yes'
1:'no' == True:'no'
1.0:'maybe' == True:'maybe' (because 1, 1.0 and True all evaluated to boolian True)

Arpith S
  • 111
  • 4
  • 15
  • *"What is happening in your case is that all the keys in your dictionary evaluate to the boolean value True."* - No, they all hash to 1 – Nick is tired Apr 24 '20 at 13:02