Junior — Middle
How can you determine if a specific element is contained in a Python set?
sobes.tech AI
Answer from AI
In Python, to check if an element is in a set, the in operator is used. It is very efficient because sets are implemented based on hash tables.
Example:
my_set = {1, 2, 3, 4}
if 3 in my_set:
print("Element 3 is present in the set")
else:
print("Element 3 is not present in the set")
If the element is in the set, the expression 3 in my_set will return True, otherwise — False. This is the simplest and fastest way to check for the presence of an element.