Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Check If an Element Exists in a Python Set

  • 24 Jul, 2025
  • Com 0
How to Check If an Element Exists in a Python Set

The most Pythonic way to check if an element exists is to use the in operator. Sets provide O(1) average time complexity for membership checks due to their underlying hash table implementation.

Check If an Element Exists in a Python Set

The in operator returns True if the set contains an element and False otherwise.

Syntax

element in set

Basic membership check

Let’s declare a Set named “stocks” and check for two different elements, and see if the set contains those elements.

stocks = {"Kaynes", "Dixon", "PGEL"}

print("Kaynes" in stocks)
# Output: True

print("Syrma" in stocks)
# Output: False

Case sensitivity

Case sensitivity in Python set contains

The in operator checks for case-sensitivity as well. So, lowercase and uppercase make a huge difference.

langs = {"Python", "Php", "JavaScript"}

print("php" in langs)
# Output: False

print("Php" in langs)
# Output: True

Mixed types

What if the set contains different types of elements, and we check for a specific type? If it exists, returns True; otherwise, False.

data = {21, "Krunal", (19, 10)}

print(21 in data)
# Output: True

print("Krunal" in data)
# Output: True

print((19, 21) in data)
# Output: False

print((19, 10) in data)
# Output: True

print([1, 2] in data)
# TypeError: unhashable type: 'list'

Sets can only contain hashable types. In the last line, we are checking for a list, so it threw a TypeError: unhashable type: ‘list’ because the list is an unhashable type.

Empty Set

If the input Set is empty and you try to check any element against it, it always returns False.

data = {}

print(21 in data)

# Output: False

None as an element

Since None is hashable, it qualifies to be an element in a set. What if we want to check None as an element? Well, if the Set contains None, the “in” operator returns True for the existence of None; otherwise, it returns False.

null_set = {19, None, "khushi"}

print(None in null_set)  

# Output: True

Alternate approaches

The following are alternative approaches that you can use, but they are less reliable than the “in” operator.

  1. Using the “not in” operator
  2. Using operator.countOf() method

Approach 1: Using the not in operator

The not in operator is the negation of the in operator. So, not in operator returns False if the set contains an element. If the set does not contain an element, it returns True.

main_set = {19, 21, "Kaynes", -10, True}

print(48 not in main_set)
# Output: True

print("Kaynes" not in main_set)
# Output: False

Since, the Kaynes is included in the main_set, it returns False. 48 is not included in the main_set, so it returns True.

Approach 2: Using an operator.countOf()

The operator.countOf() method checks if specific elements are present in the set. If the element is present in the set, the .countOf() method returns True, otherwise False.

import operator as op

main_set = {19, 21, "Kaynes", -10, True}

print(op.countOf(main_set, 21) > 0)
# Output: True

print(op.countOf(main_set, 46) > 0)
# Output: False

That’s all!

Post Views: 103
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Python XOR: How to Use Bitwise XOR Operator
How to Convert NumPy Array of Floats to an Array of Integers

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend