What is a set in Python
A set in Python is an embedded data type that is an unordered collection of unique elements. Arrays are widely used to remove duplicates, perform mathematical operations, and optimize element search.
Main characteristics of sets
Uniqueness of the elements: Each element can be present in the set only once. If you try to add a duplicate, it will be automatically ignored.
Disorder: The elements of the set do not have a specific order, so you cannot access them by index.
Changeability: Sets can be modified after creation to add and remove elements.
Hashable elements: Only immutable data types (numbers, strings, tuples) can be added to a set.
Ways to create sets
Creation using curly braces
my_set = {1, 2, 3, 4, 5}
empty_set = set() # Empty set
string_set = {'apple', 'banana', 'orange'}
Creation using the set() function
# From
my_list = [1, 2, 3, 3, 4, 4, 5]
my_set = set(my_list)
print(my_set) # {1, 2, 3, 4, 5}
# From the string
text_set = set('hello')
print(text_set) # {'h', 'e', 'l', 'o'}
# From the tuple
tuple_set = set((1, 2, 3))
print(tuple_set) # {1, 2, 3}
Creating a set using a generator
# Set of squares of numbers from 1 to 5
squares = {x**2 for x in range(1, 6)}
print(squares) # {1, 4, 9, 16, 25}
Basic methods of working with sets
Adding elements
add() adds one element to a set
my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # {1, 2, 3, 4}
update() adds several elements from an iterable object
my_set = {1, 2, 3}
my_set.update([4, 5, 6])
print(my_set) # {1, 2, 3, 4, 5, 6}
# You can pass multiple arguments
to my_set.update([7, 8], {9, 10})
print(my_set) # {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Deleting elements
remove() removes the element, raises a KeyError if the element is not found
my_set = {1, 2, 3, 4}
my_set.remove(3)
print(my_set) # {1, 2, 4}
# my_set.remove(5) # KeyError: 5
discard() deletes the element, does not cause an error if the element is missing
my_set = {1, 2, 3, 4}
my_set.discard(3)
my_set.discard(5) # There will be no error
print(my_set) # {1, 2, 4}
pop() removes and returns an arbitrary element
my_set = {1, 2, 3, 4}
removed_element = my_set.pop()
print(f"Element removed: {removed_element}")
print(my_set) # Remaining elements
clear() clears the set completely
my_set = {1, 2, 3, 4}
my_set.clear()
print(my_set) # set()
Copying sets
copy() creates a shallow copy of the set
original_set = {1, 2, 3}
copied_set = original_set.copy()
copied_set.add(4)
print(original_set) # {1, 2, 3}
print(copied_set) # {1, 2, 3, 4}
Operations on sets
Union
set1 = {1, 2, 3}
set2 = {3, 4, 5}
# Using the operator |
union_set = set1 | set2
print(union_set) # {1, 2, 3, 4, 5}
# Using the union() method
union_set = set1.union(set2)
print(union_set) # {1, 2, 3, 4, 5}
Intersection
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
# Using the operator &
intersection_set = set1 & set2
print(intersection_set) # {3, 4}
# Using the intersection() method
intersection_set = set1.intersection(set2)
print(intersection_set) # {3, 4}
Difference
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
# Using the operator -
difference_set = set1 - set2
print(difference_set) # {1, 2}
# Using the method difference()
difference_set = set1.difference(set2)
print(difference_set) # {1, 2}
Symmetric difference (symmetric_difference)
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
# Using the operator ^
symmetric_diff = set1 ^ set2
print(symmetric_diff) # {1, 2, 5, 6}
# Using the method symmetric_difference()
symmetric_diff = set1.symmetric_difference(set2)
print(symmetric_diff) # {1, 2, 5, 6}
Verification of affiliation and relationships
Checking the occurrence of an element
my_set = {1, 2, 3, 4, 5}
print(3 in my_set) # True
print(6 in my_set) # False
print(7 not in my_set) # True
Checking subsets and supersets
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
print(set1.issubset(set2)) # True - set1 is a subset of set2
print(set2.issuperset(set1)) # True - set2 is a superset of set1
print(set1.isdisjoint(set2)) # False - sets intersect
Built-in functions for working with sets
my_set = {1, 2, 3, 4, 5}
print(len(my_set)) #5 - number
of print elements(max(my_set)) #5 - Maximum element
print(min(my_set)) #1 - the minimum element
print(sum(my_set)) #15 - sum of all elements
print(sorted(my_set)) # [1, 2, 3, 4, 5] - sorted list
Immutable sets (frozenset)
Python also provides an immutable version of the set frozenset. Such sets cannot be changed after creation, but they can be used as dictionary keys or elements of other sets.
# Creating frozenset
immutable_set = frozenset([1, 2, 3, 4])
print(immutable_set) # frozenset({1, 2, 3, 4})
# Using my_dict as
a dictionary key = {frozenset([1, 2]): 'value1', frozenset([3, 4]): 'value2'}
print(my_dict)
# Set of frozenset
set_of_frozensets = {frozenset([1, 2]), frozenset([3, 4])}
print(set_of_frozensets)
Practical usage examples
Removing duplicates from the list
numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = list(set(numbers))
print(unique_numbers) # [1, 2, 3, 4, 5]
Search for common items in lists
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
common_elements = list(set(list1) & set(list2))
print(common_elements) # [4, 5]
Filtering of unique words in the text
text = "python programming python language programming"
words = text.split()
unique_words = set(words)
print(unique_words) # {'python', 'programming', 'language'}
Sets in Python are a powerful tool for working with unique data and performing mathematical operations on collections. They provide high performance when checking the occurrence of elements and are indispensable for solving problems related to duplicate removal and data intersection analysis.