A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-set-issubset-method/ below:

Python Set issubset() Method - GeeksforGeeks

Python Set issubset() Method

Last Updated : 01 Nov, 2023

Python set issubset() method returns True if all elements of a set A are present in another set B which is passed as an argument, and returns False if all elements are not present in Python.

Python Set issubset() Method Syntax

Syntax: set_obj.issubset(other_set)

Parameter:

Return: bool

Python set issubset() Method Example

This code checks if set s2 is a subset of set s1 and prints True if it is.

Python3
s1 = {1, 2, 3, 4, 5}
s2 = {4, 5}
print(s2.issubset(s1))

Output:

True
Python set issubset() method How Python set issubset() Work

In this code, it checks whether set A is a subset of set B and then whether set B is a subset of set A. The first print statement returns True because all elements of set A are also in set B. The second print statement returns False because set B contains elements that are not in set A.

Python3
A = {4, 1, 3, 5}
B = {6, 0, 4, 1, 5, 0, 3, 5}

print(A.issubset(B))
print(B.issubset(A))

Output

True
False
Working with three-set using issubset()

In this code, It checks if one set is a subset of another set using the .issubset() method.

Python3
A = {1, 2, 3}
B = {1, 2, 3, 4, 5}
C = {1, 2, 4, 5}

print(A.issubset(B))
print(B.issubset(A))


print(A.issubset(C))
print(C.issubset(B))

Output

True
False
False
True


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4