To check if a specific item is present in a Set in Python, you can use the in
keyword.
In this example,
my_set
containing elements of a specific type.in
keyword to check if a specific item is present in the Set.in
keyword returns True
if the item is present in the Set, otherwise it returns False
.# Define Set with items
my_set = {"apple", "banana", "orange", "grape", "melon"}
# Check if item is present
item = "banana"
is_present = item in my_set
# Print result
print(f'Is {item} present in the Set? {is_present}')
Is banana present in the Set? True
In this example,
my_set
containing elements of a specific type.in
keyword to check if a specific item is not present in the Set.in
keyword returns False
if the item is not present in the Set.# Define Set with items
my_set = {"apple", "banana", "orange", "grape", "melon"}
# Check if item is not present
item = "peach"
is_not_present = item not in my_set
# Print result
print(f'Is {item} not present in the Set? {is_not_present}')
Is peach not present in the Set? True
In this tutorial, we learned How to check if a Specific Item is present in the Set in Python language with well detailed examples.