0 works

Check if a key already exists in a dictionary


dashboard+ Project

Posted by @bravocoder #python

Code snippet:

if 'key1' in dict:
  print "blah"
else:
  print "boo"
content_copyCopy to Clipboard

More details:

Will be much faster as it uses the dictionary's hashing as opposed to doing a linear search, which calling keys would do.

Code snippet source:

https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary

>> Browse more code snippets

more_vert