+ 3
Can anyone explain the output of the below mentioned code??
my_dict = {True:'yes',1:'no',1.0:'maybe'} print(my_dict) Output: { True : 'maybe'} I'm not able to understand why the entire dictionary is not printed as the output
7 Answers
+ 3
It is actually pretty confusing due to the fact that you are using keywords and mixing integers with decimals.
Python does a lot of stuff the way it wants it to. I would prefer a more explicit definition.
Try using keys and values that you can easily follow, like names and ages.
+ 2
I think dictionaries filter their key duplicates by their equality.
+ 2
Thank you for your explanation... But i'm still confusedđ
Why the keyword True is printed with output 'maybe' and not with its original value 'yes' ?!
+ 2
Actually this question was asked in a challenge... But I agree that booleans should not be used as keys in a dictionary.
+ 1
SC Sharma it could be because you 1.0:'maybe' was put there later and program treated it as reassigning to True:'yes', because 1.0 == True.
+ 1
It's a terrible question hahah. You can use online python compilers to try the challenges later
0
Try this format (released is the dict).
released = {
    "iphone 3G" : 2008,
    "iphone 3GS" : 2009,
    "iphone 4" : 2010,
    "iphone 4S" : 2011,
    "iphone 5" : 2012
  }
print(released)Â # Outputs all keys and values
print(len(released))Â # Outputs 5
print("iphone" in released) # Prints False
print("iphone 5" in released) # Prints True