Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
Python sets have several built-in methods for performing operations on sets. Here, we'll go through some of the most commonly used set methods:
add(element)
: Adds an element to the set.my_set = {1, 2, 3} my_set.add(4) print(my_set) # Output: {1, 2, 3, 4}
update(iterable)
: Adds multiple elements from an iterable (e.g., list, tuple, set) to the set.my_set = {1, 2, 3} my_set.update([3, 4, 5]) print(my_set) # Output: {1, 2, 3, 4, 5}
remove(element)
: Removes an element from the set. Raises a KeyError if the element is not found.my_set = {1, 2, 3} my_set.remove(1) print(my_set) # Output: {2, 3}
discard(element)
: Removes an element from the set if it is present. Does nothing if the element is not found.my_set = {1, 2, 3} my_set.discard(1) print(my_set) # Output: {2, 3}
pop()
: Removes and returns an arbitrary element from the set. Raises a KeyError if the set is empty.my_set = {1, 2, 3} print(my_set.pop()) # Output: 1 (or 2, 3; the result is arbitrary)
clear()
: Removes all elements from the set.my_set = {1, 2, 3} my_set.clear() print(my_set) # Output: set()
union(iterable)
: Returns a new set containing elements from the set and the iterable. You can also use the |
operator.set_a = {1, 2, 3} set_b = {3, 4, 5} result = set_a.union(set_b) print(result) # Output: {1, 2, 3, 4, 5}
intersection(iterable)
: Returns a new set containing elements present in both the set and the iterable. You can also use the &
operator.result = set_a.intersection(set_b) print(result) # Output: {3}
difference(iterable)
: Returns a new set containing elements present in the set but not in the iterable. You can also use the -
operator.result = set_a.difference(set_b) print(result) # Output: {1, 2}
symmetric_difference(iterable)
: Returns a new set containing elements present in either the set or the iterable, but not in both. You can also use the ^
operator.result = set_a.symmetric_difference(set_b) print(result) # Output: {1, 2, 4, 5}
issubset(iterable)
: Returns True if the set is a subset of the specified iterable, otherwise False.set_c = {1, 2} print(set_c.issubset(set_a)) # Output: True
Common Set Operations in Python:
# Example set1 = {1, 2, 3} set2 = {2, 3, 4} union_set = set1.union(set2)
Python set add()
Method:
add()
method adds an element to a set.# Example my_set = {1, 2, 3} my_set.add(4)
remove()
and discard()
Methods in Python Sets:
remove()
method removes a specified element, and discard()
removes an element if it exists.# Example my_set = {1, 2, 3} my_set.remove(2) # Removes 2 my_set.discard(3) # Removes 3
union()
and intersection()
Methods in Python Sets:
union()
method returns the union of two sets, and intersection()
returns their intersection.# Example set1 = {1, 2, 3} set2 = {2, 3, 4} union_set = set1.union(set2) intersection_set = set1.intersection(set2)
difference()
and symmetric_difference()
in Python Sets:
difference()
method returns elements present in the first set but not in the second, and symmetric_difference()
returns elements in either set, but not both.# Example set1 = {1, 2, 3} set2 = {2, 3, 4} difference_set = set1.difference(set2) symmetric_difference_set = set1.symmetric_difference(set2)
Subset and Superset Methods for Sets in Python:
issubset()
and issuperset()
check if one set is a subset or superset of another.# Example set1 = {1, 2} set2 = {1, 2, 3, 4} is_subset = set1.issubset(set2) # True is_superset = set2.issuperset(set1) # True
Set Comprehension vs Set Methods in Python:
# Example with set comprehension set1 = {x for x in range(1, 6)} set2 = {x for x in range(4, 9)} # Example with set methods union_set = set1.union(set2) intersection_set = set1.intersection(set2)