Python Practice › List Programs › Program 39
Check if an element exists
Write a Python program to check whether an element exists in a list.
Input
[3, 9, 27], check 9 and 4
Output
9 exists 4 does not exist
Approach
- Use
in.
Solution
numbers = [3, 9, 27]for x in [9, 4]: print(x, "exists" if x in numbers else "does not exist")Output
9 exists 4 does not exist
Learn the concept
New to this topic? Read the lesson, then come back.