In Python, a set is an unordered collection of unique elements. Sets are useful for performing mathematical set operations, such as union, intersection, and difference. This article explores how to define sets and perform various operations on them in Python.
Sets can be defined using curly braces {}
or the set()
constructor. Note that a set does not allow duplicate values, so duplicate elements will be automatically removed when the set is created.
# Defining a set using curly braces fruits = {"apple", "banana", "cherry"} print(fruits) # Outputs: {'apple', 'banana', 'cherry'} # Defining a set using the set() constructor numbers = set([1, 2, 3, 4, 4, 5]) print(numbers) # Outputs: {1, 2, 3, 4, 5} (duplicates are removed)
As shown in the examples, sets automatically remove duplicate values and do not maintain any specific order.
Python provides several built-in operations for working with sets. These operations include union, intersection, difference, and symmetric difference, among others.
The union operation combines all elements from two sets, returning a new set with all unique elements from both sets.
# Union of two sets set1 = {1, 2, 3} set2 = {3, 4, 5} union_set = set1 | set2 # Alternatively, you can use set1.union(set2) print(union_set) # Outputs: {1, 2, 3, 4, 5}
The intersection operation returns a new set that contains only the elements that are common to both sets.
# Intersection of two sets set1 = {1, 2, 3} set2 = {3, 4, 5} intersection_set = set1 & set2 # Alternatively, you can use set1.intersection(set2) print(intersection_set) # Outputs: {3}
The difference operation returns a set that contains elements that are present in the first set but not in the second set.
# Difference of two sets set1 = {1, 2, 3} set2 = {3, 4, 5} difference_set = set1 - set2 # Alternatively, you can use set1.difference(set2) print(difference_set) # Outputs: {1, 2}
The symmetric difference operation returns a set that contains all elements from both sets, except for the elements that are common to both.
# Symmetric Difference of two sets set1 = {1, 2, 3} set2 = {3, 4, 5} symmetric_difference_set = set1 ^ set2 # Alternatively, you can use set1.symmetric_difference(set2) print(symmetric_difference_set) # Outputs: {1, 2, 4, 5}
Python sets also support other operations such as checking if a set is a subset or superset of another set, and if two sets are disjoint (i.e., have no common elements).
Use the issubset()
method to check if one set is a subset of another, and the issuperset()
method to check if one set is a superset of another.
# Checking for subset and superset set1 = {1, 2, 3} set2 = {1, 2, 3, 4, 5} set3 = {4, 5} print(set1.issubset(set2)) # Outputs: True (set1 is a subset of set2) print(set2.issuperset(set1)) # Outputs: True (set2 is a superset of set1) print(set2.issuperset(set3)) # Outputs: True (set2 is a superset of set3)
You can use the isdisjoint()
method to check if two sets have no elements in common.
# Checking if two sets are disjoint set1 = {1, 2, 3} set2 = {4, 5, 6} set3 = {3, 4, 5} print(set1.isdisjoint(set2)) # Outputs: True (set1 and set2 are disjoint) print(set1.isdisjoint(set3)) # Outputs: False (set1 and set3 are not disjoint)
Sets in Python provide a versatile and efficient way to perform a variety of operations like union, intersection, difference, and more. They are particularly useful when you need to work with collections of unique elements or perform mathematical set operations. By understanding the basic set operations and methods, you can use sets effectively in your Python programs.