Open In App

How to check if a set contains an element in Python?

Last Updated : 25 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to check if a set contains an element in python.

Method: 1 Using in operator

This is an membership operator used to check whether the given value is present in set or not. It will return True if the given element is present in set , otherwise False.

Syntax:

element in set

where

  • set is an input set
  • element is the value to be checked

Example: Check if an element is present in a set

Python3




# import random module
import random
 
# create a set with integer elements
data = {7058, 7059, 7072, 7074, 7076}
 
 
# check 7058
print(7058 in data)
 
# check 7059
print(7059 in data)
 
# check 7071
print(7071 in data)


Output:

True
True
False

Method 2: Using not in operator

This is an membership operator used to check whether the given value is present in set or not. It will return True if the given element is not present in set, otherwise False

Syntax:

element not in set

where,

  • set is an input set
  • element is the value to be checked

Example: Check if an element is present in a set

Python3




# import random module
import random
 
# create a set with integer elements
data = {7058, 7059, 7072, 7074, 7076}
 
 
# check 7058
print(7058 not in data)
 
# check 7059
print(7059 not in data)
 
# check 7071
print(7071 not in data)


Output:

False
False
True

Method 3: Using Counter() Function

Python3




from collections import Counter
 
# create a set with integer elements
data = {7058, 7059, 7072, 7074, 7076}
 
freq = Counter(data)
# check 7058
print(7058 in freq.keys())
 
# check 7059
print(7059 in freq.keys())
 
# check 7071
print(7071 in freq.keys())


Output

True
True
False

Time Complexity:O(N)

Auxiliary Space: O(N)

Method #4 : Using operator.countOf() method.

Python3




import operator as op
# create a set with integer elements
data = {7058, 7059, 7072, 7074, 7076}
 
# check 7058
print(op.countOf(data, 7058) > 0)
 
# check 7059
print(op.countOf(data, 7059) > 0)
 
# check 7071
print(op.countOf(data, 7071) > 0)


Output

True
True
False

Time Complexity: O(n)

Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Python program to check if the list contains three consecutive common numbers in Python
Our task is to print the element which occurs 3 consecutive times in a Python list. Example : Input : [4, 5, 5, 5, 3, 8] Output : 5 Input : [1, 1, 1, 64, 23, 64, 22, 22, 22] Output : 1, 22 Approach : Create a list.Create a loop for range size – 2.Check if the element is equal to the next element.Again check if the next element is equal to the next
3 min read
Python | Test if string contains element from list
We are given a String and our task is to test if the string contains elements from the list. Example: Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : TrueNaive Approach checking each word in the string Here we are splitting the string into list of words and then matchi
8 min read
Python | Check if list contains consecutive numbers
Given a list of numbers, write a Python program to check if the list contains consecutive integers. Examples: Input : [2, 3, 1, 4, 5] Output : True Input : [1, 2, 3, 5, 6] Output : False Let's discuss the few ways we can do this task. Approach #1 : using sorted() This approach uses sorted() function of Python. We compare the sorted list with list o
3 min read
Python | Check whether string contains only numbers or not
Given a string, write a Python program to find whether a string contains only numbers or not. Let's see a few methods to solve the above task. Check if String Contains Only Numbers Check if String Contains Only Numbers using isdigit() method Python String isdigit() method returns “True” if all characters in the string are digits, Otherwise, It retu
4 min read
Python | Ways to check if given string contains only letter
Given a string, write a Python program to find whether a string contains only letters and no other keywords. Let's discuss a few methods to complete the task. Method #1: Using isalpha() method C/C++ Code # Python code to demonstrate # to find whether string contains # only letters # Initialising string ini_str = "ababababa" # Printing ini
3 min read
Check if directory contains files using python
Finding if a directory is empty or not in Python can be achieved using the listdir() method of the os library. OS module in Python provides functions for interacting with the operating system. This module provides a portable way of using operating system dependent functionality. Syntax: os.listdir(<directory path>) Returns: A list of files pr
3 min read
Python - Check if Tuple contains only K elements
Sometimes, while working with Python tuples, we can have a problem in which we need to test if any tuple contains elements from set K elements. This kind of problem is quite common and have application in many domains such as web development and day-day programming. Let's discuss certain ways in which this task can be done. Input : test_tuple = (1,
3 min read
Check if dataframe contains infinity in Python - Pandas
Prerequisites: Pandas There are various cases where a data frame can contain infinity as value. This article discusses how we can keep track of infinities in our data frame. ApproachImport moduleCreate a data frame, for this article, it is done using a dictionary. For including infinity in the data, import NumPy module, and use np.inf for positive
2 min read
Python Check if the List Contains Elements of another List
In Python programming, there can be a case where we have to check whether a list contains elements of Another List or not. In that case, we can follow several approaches for doing so. In this article, we will be discussing several approaches to check whether a list contains elements of another list or not. Python List Contains Elements of another L
2 min read
Python | Check if list contains all unique elements
Some list operations require us to check if all the elements in the list are unique. This usually happens when we try to perform the set operations in a list. Hence this particular utility is essential at these times. Let's discuss certain methods by which this can be performed. Method #1: Naive Method Solutions usually start from the simplest meth
8 min read
Practice Tags :