Key in dictionary

Python, Dictionary · Oct 16, 2020

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

More like this

  • Find keys with value

    Finds all keys in the provided dictionary that have the given value.

    Python, Dictionary · Nov 2, 2020

  • Find key of value

    Finds the first key in the provided dictionary that has the given value.

    Python, Dictionary · Nov 2, 2020

  • Get nested value

    Retrieves the value of the nested key indicated by the given selector list from a dictionary or list.

    Python, Dictionary · Oct 28, 2020