Files
30-seconds-of-code/snippets/python/s/key-in-dict.md
2023-05-07 16:07:29 +03:00

409 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Key in dictionary snippet python
dictionary
rocky-mountains 2020-10-16T21:30:49+03:00

Checks if the given key exists in a dictionary.

  • Use the in operator to check if d contains key.
def key_in_dict(d, key):
  return (key in d)
d = {'one': 1, 'three': 3, 'five': 5, 'two': 2, 'four': 4}
key_in_dict(d, 'three') # True