Check if Given Element is Present in Set in Python

Check if Given Element is Present in Set

To check if given element is present in the Set, use Python in keyword, and then pass both the element and set as operands to the in keyword.

The boolean expression to check if element e is present in the set x is

e in x

Example

In the following program, we take a set x with fruit names, and check if the string "apple" is present in this set.

Python Program

x = {"apple", "banana", "cherry", "mango"}
e = "apple"
if e in x:
    print("Element is present in the set")
else:
    print("Element is not present in the set")
Run

Output

Element is present in the set

We have used Python If statement with the boolean expression “element in set” as condition.

Summary

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