0

Please advise what is the correct way to verify if key x is stored in a dictionary dict. and if it is, set a value check to True and otherwise it will be set to False.

check = True

try :
   y = d.x
except KeyError as e
   check = False

Or is it the right way to do one the following?

  1. check = dict.contains(x)
  2. check = dict.has_key(x)
  3. check = x in dict
  4. check = x.is_elem(dict)
sshashank124
  • 31,495
  • 9
  • 67
  • 76
Eugene_S
  • 110
  • 1
  • 1
  • 10

1 Answers1

1

The most common approach seems to be x in d

Oleksi
  • 12,947
  • 4
  • 56
  • 80