Check if a Value is in List in Python

Check if a Value is in List

To check if a value is present in given list in Python, use Python in keyword with If conditional statement.

Code Snippet

The following is a simple code snippet to check if the value x is in the list myList.

if x in myList:
    #code

Examples

Check If String is in List

In the following example, we take a list of strings fruits, and check if the string 'mango' is present in the list fruits.

Python Program

fruits = ['apple', 'banana', 'cherry', 'mango']
x = 'mango'
if x in fruits:
    print('value is present in the list.')
Run

Output

value is present in the list.

Check If Number is in List

In the following example, we take a list of numbers nums, and check if the number 82 is present in the list nums.

Python Program

nums = [41, 62, 70, 82, 100]
x = 82
if x in nums:
    print('value is present in the list.')
Run

Output

value is present in the list.

Check If Number is in List or Not

We may use if-else statement, and proceed with the required actions when the value is in the list, or when the value is not in the list.

In the following example, we take a list of numbers nums, and check if the number 99 is present in the list nums.

Python Program

nums = [41, 62, 70, 82, 100]
x = 99
if x in nums:
    print('value is present in the list.')
else:
    print('value is not present in the list.')
Run

Output

value is not present in the list.

Summary

In this tutorial of Python Examples, we learned how to check if given value is present in the list or not, using Python in keyword.