×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python - Check if Item Exists in List

Last Updated : May 01, 2025

In Python, you can check if an item exists in a list using the in keyword. In this chapter, we will learn how to perform membership tests and use conditional checks to find an item with the help of examples.

Check if Item Exists in List Using in Keyword

The simplest way to check if an item exists in a list is by using the in keyword. It returns True if the item is found, otherwise False.

Example

In this example, we are checking if the city 'Paris' exists in the list of cities.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo"]

# Checking if 'Paris' is in the list
if "Paris" in cities:
    print("Paris is in the list.")
else:
    print("Paris is not in the list.")

The output of the above code would be:

Paris is in the list.

Check if Item Exists in List Using not in Keyword

The not in keyword is used to check if an item does not exist in the list.

Example

In this example, we are checking if the city 'Berlin' does not exist in the list of cities.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo"]

# Checking if 'Berlin' is not in the list
if "Berlin" not in cities:
    print("Berlin is not in the list.")
else:
    print("Berlin is in the list.")

The output of the above code would be:

Berlin is not in the list.

Check if Item Exists in List Using a Function

You can create a function to check for item existence which will help to make code reusable and organized.

Example

In this example, we define a function that checks whether a city is in the list.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo"]

# Defining a function to check if a city exists
def check_city(city_name):
    return city_name in cities

# Using the function
print(check_city("Tokyo"))   # True
print(check_city("Berlin"))  # False

The output of the above code would be:

True
False

Check if Item Exists in List (Case-Insensitive)

To check for an item in a case-insensitive way, you can convert both the list elements and the search item to lowercase before comparison.

Example

In this example, we are checking if 'paris' exists in the list, regardless of case.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo"]

# Converting list items to lowercase
lower_cities = [city.lower() for city in cities]

# Checking if 'paris' (in lowercase) is in the list
if "paris".lower() in lower_cities:
    print("Paris is in the list (case-insensitive).")
else:
    print("Paris is not in the list.")

The output of the above code would be:

Paris is in the list (case-insensitive).

Exercise

Select the correct option to complete each statement about checking item existence in a Python list.

  1. Which keyword is used to check if an item exists in a list?
  2. What does the expression 'apple' in fruits return if fruits = ['apple', 'banana']?
  3. Which of the following correctly checks if the number 5 is not in the list nums = [1, 2, 3, 4]?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.