Python Set

Python Set is like a dictionary, an unordered collection of keys, held without any values. The set type is mutable , the contents can be changed using methods like add() and remove() . Sets do not contain duplicates, every element in a Python set can have only one occurrence in that particular set, no multiple occurrences are allowed. There are currently two built-in set types, Set and Frozenset . example
set_num = set("Hello World!") print(set_num)
output
{'o', 'H', 'r', 'l', ' ', 'd', 'e', '!', 'W'}

Create a Set from Tuple

example
set_dir = set(('East','West','North','South')) print(set_dir)
output
{'West', 'South', 'North', 'East'}

Create a Set from List

example
set_dir = set(['East','West','North','South']) print(set_dir)
output
{'South', 'West', 'North', 'East'}

Sets are Mutable

The set type is mutable, the contents can be changed using methods like add() and remove().

example
set_col = set({'Red','Blue','Green'}) print(set_col) set_col.add('White') print(set_col)
output
{'Green', 'Blue', 'Red'} {'White', 'Green', 'Blue', 'Red'}

Set.remove() remove elements from set.

example
set_col = set({'Red','Blue','Green'}) print(set_col) set_col.remove('Blue') print(set_col)
output
{'Blue', 'Green', 'Red'} {'Green', 'Red'}

Set Length

len() function returned the length of a set.

example
set_col = set({'Red','Blue','Green'}) print(len(set_col))
output
3

Union of Sets

In Python, Union operation can be performed on two Python sets using the operator or by using the method union(). Union of two sets x and y is another set that contains all distinct elements in sets x and x.

Using | Operator

example
x = set({1,2,3,4,5}) y = set({3,4,5,6,7}) z = x y print(z)
output
{1, 2, 3, 4, 5, 6, 7}

Using union()

example
x = set({1,2,3,4,5}) y = set({3,4,5,6,7}) z = x.union(y) print(z)
output
{1, 2, 3, 4, 5, 6, 7}

Intersection of Sets

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

Using & operator

example
x = set({1,2,3,4,5}) y = set({3,4,5,6,7}) z = x & y print(z)
output
{3, 4, 5}

Using intersection()

example
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 of Sets

The difference() method returns the set difference of two sets. In Python, Difference operation can be performed on two Python sets using the operator - or by using the method difference().

Using - operator

example
x = set({1,2,3,4,5}) y = set({3,4,5,6,7}) z = x-y print(z)
output
{1, 2}

Using difference()

example
x = set({1,2,3,4,5}) y = set({3,4,5,6,7}) z = x.difference(y) print(z)
output
{1, 2}

Symmetric Difference of Sets

Symmetric difference of the sets is the set of elements those are not common to both the sets. The symmetric_difference() method using.

Using symmetric_difference()

example
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

A frozenset value is immutable: it's set up with the frozenset() function call, but once it's set up its contents cannot be altered.

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