The 'in' Operator


I can’t wrap my head around why ‘my_dict[key]’ is used to print the value associated with a key. It seems like it should simply be ‘value’. Wouldn’t that make a lot more sense?

for key in my_dict:
  print key, my_dict[key]


but to get value from dictionary we need to use the key:

example = {'first': 'entry'}
print example['first']

you do the same in the loop.

looping over the dictionary will give us the keys, we can’t access the values directly


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.