• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Check If Value is in List Using Python

How to Check If Value is in List Using Python

August 26, 2022 Leave a Comment

To check if a value is in a list using Python, the easiest way is with the in operator.

value = 1
l = [0, 1, 2, 3, 4]

if value in l:
    print("value is in list")
else:
    print("value is not in list")

#Output:
value is in list

When working with collections of data in Python, the ability to perform certain checks of different conditions is valuable.

One such case is if you want to check if a value is in a list in Python.

To check if a value is in a list using Python, the easiest way is with the in operator.

If the value you want to check is in the list, then in returns True.

Otherwise in returns False.

Below shows you a simple example of how you can check if a value is in a list using Python.

value = 1
l = [0, 1, 2, 3, 4]

if value in l:
    print("value is in list")
else:
    print("value is not in list")

#Output:
value is in list

How to Check if Value is Not in List in Python

To check if a value is not in a list using Python, you can use the in operator combined with the not operator.

Just put not in front of in to negate the returned value from in.

Below shows you a simple example of how you can check if a value is not in a list using Python.

value = 5
l = [0, 1, 2, 3, 4]

if value not in l:
    print("value is not in list")
else:
    print("value is in list")

#Output:
value is not in list

Hopefully this article has been useful for you to learn how to check if a value is in a list using Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Remove Non-Empty Directory
  • 2.  Python getsizeof() Function – Get Size of Object
  • 3.  How to Convert pandas Column dtype from Object to Category
  • 4.  Count Number of Files in Directory with Python
  • 5.  Count Unique Values in pandas DataFrame
  • 6.  Apply Function to All Elements in List in Python
  • 7.  Get Day of Week from Datetime in pandas DataFrame
  • 8.  Using Python to Get Domain from URL
  • 9.  Ceiling Division in Python
  • 10.  How to Check if Character is Uppercase in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy