Check if Key Exists in Dictionary

Beginner

This tutorial is from open-source community. Access the source code

Introduction

In Python, a dictionary is a collection of key-value pairs. Each key is unique and is used to access its corresponding value. In this challenge, you will write a function that checks if a given key exists in a dictionary.

Check if Key Exists in Dictionary

Write a function key_in_dict(d, key) that takes a dictionary d and a key key as arguments and returns True if the key exists in the dictionary, False otherwise.

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

Summary

In this challenge, you learned how to check if a key exists in a dictionary in Python. You can use the in operator to check if a dictionary contains a specific key.