Python

Methods on set in Python

Click for All Topics

Methods on set in Python

In Python, sets are versatile data structures, and there are numerous methods available for manipulating and working with sets. Here’s a list of common methods you can use on sets in Python:

set.add(element): Adds an element to the set.

				
					my_set = {1, 2, 3}
print('original set:',my_set)
my_set.add(4)
print('Modified set:',my_set)

# Output -

# original set: {1, 2, 3}
# Modified set: {1, 2, 3, 4}
				
			

set.clear(): Removes all elements from the set.

				
					my_set = {1, 2, 3}
print('original set:',my_set)
my_set.clear()
print('Modified set:',my_set)

# Output -
# original set: {1, 2, 3}
# Modified set: set()
				
			

set.copy(): Returns a shallow copy of the set.

				
					my_set = {1, 2, 3}
print('original set:',my_set)
copy_of_set = my_set.copy()
print('Copied set:',copy_of_set)

# Output -
# original set: {1, 2, 3}
# Copied set: {1, 2, 3}
				
			

set.difference(other_set): Returns a new set with elements in the first set but not in the second set.

				
					set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1.difference(set2)
print('original set:',set1)
print('difference set:',difference_set)

# Output -
# original set: {1, 2, 3}
# difference set: {1, 2}
				
			

set.difference_update(other_set):Removes all elements from the set that are also in another set.

				
					set1 = {1, 2, 3}
set2 = {3, 4, 5}
print('original set:',set1)
set1.difference_update(set2)
print('Modified set:',set1)

# Output -
# original set: {1, 2, 3}
# Modified set: {1, 2}
				
			

set.discard(element): Removes the specified element from the set if present.

				
					my_set = {1, 2, 3}
print('original set:',my_set)
my_set.discard(2)
print('Modified set:',my_set)

# Output -
# original set: {1, 2, 3}
# Modified set: {1, 3}
				
			

set.intersection(other_set): Returns a new set with common elements from both sets.

				
					set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print('original set:',set1)
print('intersection set:',intersection_set)

# Output -
# original set: {1, 2, 3}
# intersection set: {3}
				
			

set.intersection_update(other_set): Updates the set with the intersection of itself and another set.

				
					set1 = {1, 2, 3}
set2 = {3, 4, 5}
print('original set:',set1)
set1.intersection_update(set2)
print('Modified set:',set1)

# Output -
# original set: {1, 2, 3}
# Modified set: {3}
				
			

set.isdisjoint(other_set): Returns True if two sets have no elements in common.

				
					set1 = {1, 2, 3}
set2 = {4, 5, 6}
is_disjoint = set1.isdisjoint(set2)
print('original set:',set1)
print('Is disjoint:',is_disjoint)

# Output -
# original set: {1, 2, 3}
# Is disjoint: True
				
			

set.issubset(other_set): Returns True if the set is a subset of another set.

				
					set1 = {1, 2}
set2 = {1, 2, 3, 4}
is_subset = set1.issubset(set2)
print('original set:',set1)
print('Is Subset:',is_subset)

# Output -
# original set: {1, 2}
# Is Subset: True
				
			

set.issuperset(other_set): Returns True if the set is a superset of another set.

				
					set1 = {1, 2, 3, 4}
set2 = {1, 2}
is_superset = set1.issuperset(set2)
print('original set:',set1)
print('Is Superset:',is_superset)

# Output -
# original set: {1, 2, 3, 4}
# Is Superset: True
				
			

set.pop(): Removes and returns an arbitrary element from the set.

				
					my_set = {1, 2, 3}
print('original set:',my_set)
popped_element = my_set.pop()
print('Modified set:',my_set)

# Output -
# original set: {1, 2, 3}
# Modified set: {2, 3}
				
			

set.remove(element): Removes the specified element from the set. Raises a KeyError if the element is not found.

				
					my_set = {1, 2, 3}
print('original set:',my_set)
my_set.remove(2)
print('Modified set:',my_set)

# Output -
# original set: {1, 2, 3}
# Modified set: {1, 3}
				
			

set.symmetric_difference(other_set): Returns a new set with elements in either set, but not both.

				
					set1 = {1, 2, 3}
set2 = {3, 4, 5}
symmetric_difference_set = set1.symmetric_difference(set2)
print('original set:',set1)
print('symmetric difference set:',symmetric_difference_set) 

# Output -
# original set: {1, 2, 3}
# symmetric difference set: {1, 2, 4, 5}
				
			

set.symmetric_difference_update(other_set): Updates the set with the symmetric difference of itself and another set.

				
					set1 = {1, 2, 3}
set2 = {3, 4, 5}
print('original set:',set1)
set1.symmetric_difference_update(set2)
print('Modified set:',set1)

# Output -
# original set: {1, 2, 3}
# Modified set: {1, 2, 4, 5}
				
			

set.union(other_set): Returns a new set with all unique elements from both sets.

				
					set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print('original set:',set1)
print('union set:',union_set) 

# Output -
# original set: {1, 2, 3}
# union set: {1, 2, 3, 4, 5}
				
			

set.update(other_set): Updates the set with the union of itself and another set.

				
					set1 = {1, 2, 3}
set2 = {3, 4, 5}
print('original set:',set1)
set1.update(set2)
print('Modified set:',set1)

# Output -
# original set: {1, 2, 3}
# Modified set: {1, 2, 3, 4, 5}