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
  • 29,826
  • 8
  • 62
  • 75
Eugene_S
  • 80
  • 1
  • 1
  • 9

1 Answers1

1

The most common approach seems to be x in d

Oleksi
  • 12,802
  • 4
  • 55
  • 79