Menu
Coddy logo textTech

Checking for Keys

Part of the Logic & Flow section of Coddy's Python journey — lesson 14 of 78.

When working with dictionaries, you often need to check if a specific key exists. Python provides simple ways to check for the presence of keys in a dictionary.

Using the in keyword:

my_dict = {'name': 'Alice', 'age': 30}

# Check if 'name' is a key in the dictionary
if 'name' in my_dict:
    print('Name exists in the dictionary')

# Check if 'city' is a key in the dictionary
if 'city' not in my_dict:
    print('City does not exist in the dictionary')

In this example, we use the in keyword to check if 'name' is one of the keys in my_dict. We also use not in to check if 'city' is not a key in the dictionary.

Using the keys() method:

my_dict = {'name': 'Alice', 'age': 30}

# Check if 'age' is in the keys
if 'age' in my_dict.keys():
    print('Age exists in the dictionary')

Here, my_dict.keys() returns a view object containing all keys in the dictionary, and we use in to check if 'age' is in that list.

challenge icon

Challenge

Easy

You are managing a dictionary of employee data, where each key is an employee's name and the value is their department. Your task is to:

  1. Check if "Alice" is a key in the dictionary.
    • If it exists, print: "Alice is in the company."
  2. Check if "John" is not a key in the dictionary.
    • If it does not exist, print: "John is not in the company."

Cheat sheet

To check if a key exists in a dictionary, use the in keyword:

my_dict = {'name': 'Alice', 'age': 30}

# Check if key exists
if 'name' in my_dict:
    print('Name exists in the dictionary')

# Check if key does not exist
if 'city' not in my_dict:
    print('City does not exist in the dictionary')

You can also use the keys() method:

# Check if key is in the list of keys
if 'age' in my_dict.keys():
    print('Age exists in the dictionary')

Try it yourself

employees = {"Alice": "HR", "Bob": "Engineering", "Diana": "Marketing"}

# Write code here
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow