Files
30-seconds-of-code/snippets/key_in_dict.md
Isabelle Viktoria Maciohsek cbc78ee450 Bake dates into snippets
2021-06-13 19:38:10 +03:00

392 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
key_in_dict dictionary,beginner 2020-10-16T21:30:49+03:00 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