Python’s Set is an unordered collection of unique elements. It is mutable and supports various operations to manipulate its content. Here are some common Set methods:
add(element): Adds an element to the Set.
clear(): Removes all elements from the Set.
copy(): Returns a shallow copy of the Set.
difference(set): Returns a new Set with the difference between the current Set and the specified Set.
difference_update(set): Removes all elements of the specified Set from the current Set.
discard(element): Removes the specified element from the Set if it is present.
intersection(set): Returns a new Set with the intersection of the current Set and the specified Set.
intersection_update(set): Updates the current Set with the intersection of itself and the specified Set.
isdisjoint(set): Returns True if the current Set and the specified Set have no common elements, otherwise False.
issubset(set): Returns True if the current Set is a subset of the specified Set, otherwise False.
issuperset(set): Returns True if the current Set is a superset of the specified Set, otherwise False.
pop(): Removes and returns an arbitrary element from the Set.
remove(element): Removes the specified element from the Set.
symmetric_difference(set): Returns a new Set with the symmetric difference between the current Set and the specified Set.
symmetric_difference_update(set): Updates the current Set with the symmetric difference of itself and the specified Set.
union(set): Returns a new Set with the union of the current Set and the specified Set.
update(set): Updates the current Set with the union of itself and the specified Set.
These methods allow for easy and efficient manipulation of Sets in Python.
here’s a summary of Python Set methods and their uses with examples:
# Creating a set my_set = {1, 2, 3} # Adding an element to the set my_set.add(4) print(my_set) # Output: {1, 2, 3, 4}
# Creating a set my_set = {1, 2, 3} # Clearing the set my_set.clear() print(my_set) # Output: set()
# Creating a set my_set = {1, 2, 3} # Copying the set new_set = my_set.copy() print(new_set) # Output: {1, 2, 3}
# Creating two sets set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} # Finding the difference between the two sets diff_set = set1.difference(set2) print(diff_set) # Output: {1, 2}
# Creating a set my_set = {1, 2, 3, 4} # Discarding an element from the set my_set.discard(3) print(my_set) # Output: {1, 2, 4}
# Creating two sets set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} # Finding the intersection of the two sets intersection_set = set1.intersection(set2) print(intersection_set) # Output: {3, 4}
# Creating two sets set1 = {1, 2, 3} set2 = {4, 5, 6} # Checking if the sets are disjoint if set1.isdisjoint(set2): print("The sets have no common elements") else: print("The sets have common elements")
Returns True if all elements of a set are in another set.
# Creating two sets set1 = {1, 2, 3} set2 = {1, 2, 3, 4, 5} # Checking if set1 is a subset of set2 if set1.issubset(set2): print("set1 is a subset of set2") else: print("set1 is not a subset of set2")
# Creating two sets set1 = {1, 2, 3, 4, 5} set2 = {2, 4} # Checking if set1 is a superset of set2 if set1.issuperset(set2): print("set1 is a superset of set2") else: print("set1 is not a superset of set2")
# Creating a set my_set = {1, 2, 3} # Popping an
Removes an element from a set; raises KeyError if the element is not present.
# Creating a set my_set = {1, 2, 3} # Removing an element from the set my_set.remove(2) print(my_set) # Output: {1, 3}
Returns the symmetric difference of two sets.
# Creating two sets set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} # Finding the symmetric difference of the two sets sym_diff_set = set1.symmetric_difference(set2) print(sym_diff_set) # Output: {1, 2, 5, 6}
Updates a set with the symmetric difference of itself and another.
# Creating two sets set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} # Updating set1 with its symmetric difference with set2 set1.symmetric_difference_update(set2) print(set1) # Output: {1, 2, 5, 6}
# Creating two sets set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} # Finding the union of the two sets union_set = set1.union(set2) print(union_set) # Output: {1, 2, 3, 4, 5, 6}
# Creating two sets set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} # Updating set1 with its union with set2 set1.update(set2) print(set1) # Output: {1, 2, 3, 4, 5, 6}
Adds an element to a set; if the element already exists, the set remains unchanged.
# Creating a set my_set = {1, 2, 3} # Adding an element to the set my_set.add(4) print(my_set) # Output: {1, 2, 3, 4}
clear(): Removes all elements from a set.
# Creating a set my_set = {1, 2, 3} # Clearing the set my_set.clear() print(my_set) # Output: set()
copy(): Returns a shallow copy of a set.
# Creating a set my_set = {1, 2, 3} # Making a copy of the set my_set_copy = my_set.copy() print(my_set_copy) # Output: {1, 2, 3}
difference(): Returns the difference of two or more sets as a new set.
# Creating two sets set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} # Finding the difference between the two sets diff_set = set1.difference(set2) print(diff_set) # Output: {1, 2}
difference_update(): Removes all elements of another set from this set.
# Creating two sets set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} # Updating set1 with its difference with set2 set1.difference_update(set2) print(set1) # Output: {1, 2}
discard(): Removes an element from a set if it is a member; if not, the set remains unchanged.
# Creating a set my_set = {1, 2, 3} # Discarding an element from the set my_set.discard(2) print(my_set) # Output: {1, 3}
intersection(): Returns the intersection of two or more sets as a new set.
# Creating two sets set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} # Finding the intersection between the two sets intersection_set = set1.intersection(set2) print(intersection_set) # Output: {3, 4}
intersection_update(): Updates a set with the intersection of itself and another.
# Creating two sets set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} # Updating set1 with its intersection with set2 set1.intersection_update(set2) print(set1) # Output: {3, 4}
isdisjoint(): Returns True if two sets have no intersection, otherwise False.
# Creating two sets set1 = {1, 2, 3} set2 = {4, 5, 6} # Checking if the sets are disjoint disjoint_sets = set1.isdisjoint(set2) print(disjoint_sets) # Output: True
issubset(): Returns True if another set contains this set, otherwise False.
# Creating two sets set1 = {1, 2, 3} set2 = {1, 2, 3, 4, 5}
# Creating three sets set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} set3 = {4, 5, 6, 7} # Finding the intersection of the three sets intersection_set = set1.intersection(set2, set3) print(intersection_set) # Output: {4}
# Creating a set my_set = {1, 2, 3, 4, 5} # Removing an element from the set my_set.remove(3) print(my_set) # Output: {1, 2, 4, 5}
# Creating two sets set1 = {1, 2, 3, 4, 5} set2 = {2, 4} # Checking if set1 is a superset of set2 if set1.issuperset(set2): print("set1 is a superset of set2") else: print("set1 is not a superset of set2")
# Creating a list with duplicates my_list = [1, 2, 3, 2, 1, 4, 5, 4] # Converting the list to a Set to remove duplicates my_set = set(my_list) # Converting the Set back to a list my_new_list = list(my_set) print(my_new_list) # Output: [1, 2, 3, 4, 5]