Python Sets

Python data types called sets work similarly to mathematical sets. They are defined as collections that are unordered and devoid of duplicate components. Duplicate entry removal and membership testing are two basic uses for sets. Similar to lists, sets store a collection of elements; however, sets differ from lists in that their elements are not duplicated and are not maintained in a certain order.
Curly braces {} can be used to create sets with a series of elements separated by commas.
For example:
set2 = {'James', 2, 3,'Python'}
S = {1,2,3,4,5}
As an alternative, you can create a set using the built-in method set(). You must use set(), not {}, to construct an empty set. A dictionary created with {} is empty.
set1 = set()
S = set()
Other data types, such lists or strings, can easily be converted into sets using the set() function. Every character becomes an element when converting from a string, and duplicate characters are only saved once.
set([9, 9, 9, 10, 10, 10-13])
set('this is a test')
set2 = set([1, 9-12, 12, 14])
s5 = set("abac")
Curly brackets can also be used to construct sets using set comprehensions, just like in list comprehensions.
Sets support various operations and methods Some common set methods include:
- add(): Expands the set by one element.
- remove(): Eliminates a component from the collection.
- discard(): Takes an item out of the collection.
- clear(): Gets rid of everything in the set.
- A copy of the set is returned by the copy() function.
The in or not in operators can check if an element is in a set. A for loop cycles across a set. The len() method returns a set’s length. You can use max(), min(), and sum() on sets.
print("Is red in set1?", "red" in set1)
print("length is", len(set2))
print("max is", max(set2))
print("min is", min(set2))
It is important to note that the index operator cannot access set members since they are not in order.
Set objects provide union, intersection, difference, and symmetric difference. Certain operators can be used to carry out these operations:
- Union (|): Produces a new set with every element from both sets in it.
- Intersection (&): Provides a new set that only includes the elements that the sets have in common.
- Difference (-): Provides a new set with first-set elements that are absent from second-set elements.
- Symmetric Difference (^): This function yields a new set with elements that are present in one set but not both.
An illustration of these operations is provided here:
Here’s an example demonstrating these operations:
# Set up sets
exam = {'Andrew', 'Kirsty', 'Beth', 'Emily', 'Sue'}
project = {'Kirsty', 'Emily', 'Ian', 'Stuart'}
# Output the basic sets
print('exam:', exam)
print('project:', project)
# Example Set Operations
print("Union:", exam | project)
print("Intersection:", exam & project)
print("Difference (exam - project):", exam - project)
print("Difference (project - exam):", project - exam)
print("Symmetric Difference:", exam ^ project)
Set functions like issubset() and issuperset() test set relationships. You may also use == to check if two sets are equal.
Python also has frozenset(), an immutable set. It may be necessary to transform nested sets into frozen sets for certain actions.