Check if specific value is in the List


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

1. Check if string "mango" is in the given 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.')

Output

value is present in the list.

2. Check if the number 82 is in the given 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.')

Output

value is present in the list.

3. Check if number 99 is in the list or not using if-else

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.')

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.