Sets in Python – All methods

A set collection in python is basically a data type made up of a collection of unordered elements. These elements can be of any data type, because sets, unlike arrays, are type independent. Sets are mutable (can be changed) and do not have duplicate copies of elements. Set values ​​are not indexed, so indexing operations cannot be performed on sets.

Working with Sets in Python

Python Set is an unordered set of keys that are stored without any values. The set type is mutable, the content can be changed using methods like Add () and Remove (). Sets do not contain duplicates, each element in a Python set can only have one occurrence in that particular set, allow multiple occurrences. There are currently two built-in sets of types: Set and Frozenset.

set_num = set("Hello World!")
print(set_num)
Output:
{'o', 'H', 'r', 'l', ' ', 'd', 'e', '!', 'W'}

Adding/Removing Set Elements in Python

Since sets are mutable, there are built-in methods to add or remove the various set elements/items.

colors = set({'Red','Blue','Yellow'})
print(colors)
colors.add('Green')
print(colors)
Output:
{'Red', 'Yellow', 'Blue'}
{'Green', 'Red', 'Yellow', 'Blue'}
colors = set({'Red','Blue','Yellow'})
print(colors)
colors.remove('Yellow')
print(colors)
Output:
{'Yellow', 'Blue', 'Red'}
{'Blue', 'Red'}

len(): The len( ) function also works for the set data collection to return the length of items.

alphabets = set({'A','B','C'})print(len(alphabets))
Output:
3

Union of Sets in Python

In Python, the Union operation can be performed on two Python sets using a statement or using the union () method. The union of two sets x and y is another set containing all the different elements in the sets x and x.

x = set({11,22,33,44,55})
y = set({4,5,6,7})
z = x.union(y)
print(z)
Output:
{33, 4, 5, 6, 7, 11, 44, 22, 55}

Intersection of sets in Python

In Python, the Union operation can be performed on two sets of Python using the & operator or using the intersection () method. The intersection of two sets x and y is another set of elements present in both sets of x and y (common to both of them).

#Using the '&' operator
x = set({1,2,3,4,5})
y = set({3,4,5,6,7})
z = x & y
print(z)

#Using the intersection( ) operator
x = set({1,2,3,4,5})
y = set({3,4,5,6,7})
z = x.intersection(y)
print(z)
Output:
{3, 4, 5}

Difference() method in python set

The difference () method returns the specified difference between the two sets. In Python, a difference operation can be performed on two sets of Python using the – operator or using the difference () method.

#Using the '&' operator
x = set({1,2,3,4,5})
y = set({3,4,5,6,7})
z = x-y
print(z)
#Using the intersection( ) operator
x = set({1,2,3,4,5})
y = set({3,4,5,6,7})
z = x.difference(y)
print(z)
Output:
{1, 2}

Symmetric set difference method in python set

 Symmetric set difference is a set of elements that are not common to both sets. The symmetric_difference () method is used.

x = set({1,2,3,4,5})
y = set({3,4,5,6,7})
z = x.symmetric_difference(y)
print(z)
Output:
{1, 2, 6, 7}

Frozenset

The frozenset value is unchanged: it is configured to call the frozenset () function, but once it is configured, its contents cannot be changed. Hence, set elements of the collection are immutable and cannot be modified. Attempting to modify a frozenset would simply return an error.

set_col = frozenset({'Red','Blue','Green'})
print(set_col)
set_col.add('White')
print(set_col)
Output:
frozenset({'Red', 'Green', 'Blue'})
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    set_col.add('White')
AttributeError: 'frozenset' object has no attribute 'add'

Update() method in python set

 Update( ) method updates the current set, by adding items from another set (or any other iterable such as list or tuple).If an item is present in both sets, only one appearance of this item will be present in the updated set.

animals = {"Deer", "Monkey", "Zebra"}
moreanimals = ["Giraffe", "Horse"]
animals.update(moreanimals)
print(animals)
Output:
{'Horse', 'Zebra', 'Giraffe', 'Monkey', 'Deer'}

clear() method in python set

Removes all elements from an iterable data type collection

set_num = set(“Hello World!”)
print(set_num)
set_num.clear()
print(set_num)

Output:
{'r', 'H', 'l', 'o', ' ', 'W', 'e', '!', 'd'}
set()

The clear( ) method clears the set and hence only returns an empty set collection consisting of no elements.

Leave a Comment