Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python Set Methods

Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python Set Methods

Set Methods in Python

Beyond adding and removing items, Python sets offer methods to compare and analyze relationships between sets — just like in mathematics.

isdisjoint(): Are the sets completely different?

This method checks if two sets have no items in common.

  • Returns True if they are completely disjoint.
  • Returns False if they share even one item.
Example 1:
				
					group_a = {"Karachi", "Lahore", "Multan"}
group_b = {"Quetta", "Peshawar"}
print(group_a.isdisjoint(group_b))


				
			

Output:

				
					True

				
			
Example 2:
				
					group_a = {"Karachi", "Lahore", "Multan"}
group_b = {"Lahore", "Peshawar"}
print(group_a.isdisjoint(group_b))

				
			

Output:

				
					False

				
			

issuperset(): Does one set contain everything from another?

This method checks if all items of the second set exist in the first one.

  • Returns True if the first set includes all elements of the second.
  • Returns False otherwise.
Example 1:
				
					languages = {"Python", "Java", "C++", "JavaScript"}
required = {"Java", "Python"}
print(languages.issuperset(required))

				
			

Output:

				
					True

				
			
Example 2:
				
					languages = {"Python", "Java", "C++", "JavaScript"}
required = {"C#", "Python"}
print(languages.issuperset(required))


				
			

Output:

				
					False

				
			

issubset(): Is one set fully contained within another?

This method checks if all items of the first set exist in the second one.

  • Returns True if the first set is fully contained in the second.
  • Returns False otherwise.
Example 1:
				
					skills = {"HTML", "CSS"}
web_stack = {"HTML", "CSS", "JavaScript", "React"}
print(skills.issubset(web_stack))


				
			

Output:

				
					True


				
			
Example 2:
				
					skills = {"HTML", "SQL"}
web_stack = {"HTML", "CSS", "JavaScript", "React"}
print(skills.issubset(web_stack))


				
			

Output:

				
					False

				
			

Summary

  • isdisjoint() → Checks for no common items.

  • issuperset() → First set must contain all items of the second.

  • issubset() → First set must be completely inside the second.

These methods are essential when comparing data across groups, such as verifying permissions, checking coverage, or validating data categories.

Keep practicing — only on Learn With Arshyan.

Scroll to Top