Set Data Type in Python Programming

Rina Mondal
5 min readJan 23, 2024

--

In Python, a Set is an unordered collection of unique elements. Sets are mutable i.e. you can add or remove elements after the set is created. In this article, we will read all the topics of set as mentioned in the diagram.

Creating a Set:

Sets can be created using curly braces{}.

# create an empty set
s = set()
print(s)

# 1D set
s1 = {1,2,3}
print(s1)

# hetrogeneous set
s3 = {1,'hello',4.5,(1,2,3)}
print(s3)

Characteristics:

  1. Unordered: Sets do not maintain the order in which elements are added. When you iterate over a set, the order of elements is not guaranteed.
# Unordered: Sets do not maintain the order of elements
unordered_set = {3, 1, 4, 1, 5, 9}
print("Unordered Set:", unordered_set)

#If you print different times, the order of output will be different each time.

2. Unique Elements: Sets only contain unique elements. Duplicate elements are not allowed. If you try to add an element that already exists in the set, it won’t be added again.

# Unique Elements: Sets only contain unique elements
unique_set = {2, 4, 6, 4, 8, 10}
print("Unique Set:", unique_set)

#O/t- Unique Set: {2, 4, 6, 8, 10}

3. Mutable: Sets are mutable, meaning you can add and remove elements after the set is created. However, the elements themselves must be immutable.

# Mutable: Sets are mutable; elements can be added and removed
mutable_set = {1, 2, 3}
print("Original Set:", mutable_set)

# Adding an element
mutable_set.add(4)
print("After Adding 4:", mutable_set)

# Removing an element
mutable_set.remove(2)
print("After Removing 2:", mutable_set)

# O/t- Original Set: {1, 2, 3}
After Adding 4: {1, 2, 3, 4}
After Removing 2: {1, 3, 4}

# set can't have mutable items
s6 = {1,2,[3,4]}
print(s6) #Output print error

4. Dynamic Size: Sets can grow or shrink in size as elements are added or removed.

# Dynamic Size: Sets can grow or shrink
dynamic_set = {1, 2, 3}
print("Original Set:", dynamic_set)
#Output
Original Set: {1, 2, 3}


# Adding elements
dynamic_set.add(4)
dynamic_set.add(5)
print("After Adding 4 and 5:", dynamic_set)
#Output
After Adding 4 and 5: {1, 2, 3, 4, 5}


# Removing elements
dynamic_set.remove(2)
print("After Removing 2:", dynamic_set)
#Output
After Removing 2: {1, 3, 4, 5}

5. No Indexing: Sets do not support indexing. You cannot access elements by their position in the set.

# No Indexing: Sets do not support indexing
# Uncommenting the line below will result in an error
# print("First Element:", dynamic_set[0])

Accessing Items:

  1. In Operator: To access items in a set, you typically use the in operator to check for membership. Sets are unordered, so they don't support indexing or slicing like lists or tuples.
set = {"apple", "banana", "cherry"}
print("banana" in set)

#Output
yes

Modify Set Data Type:

Adding Items:

Once a set is created, you cannot change its items, but you can add new items.

i. add() method: Add an item to a set

# Creating a set
s = {1, 2, 3, 4}
# Adding an item to the set
s.add(5)
print(s)

#Output
{1,2,3,4,5}

ii. update() method: you can update a set by adding multiple elements at once. The elements to be added can be provided as an iterable (such as a list or another set).

# Creating a set
s = {1, 2, 3, 4}
# Updating the set with multiple elements
s.update([5, 6, 7])
print(s)

#Output
{1,2,3,4,5,6,7}

Deleting Items: To delete a set in Python, you can use the del statement.

#Creating a set
s = {1, 2, 3, 4}

# Deleting the set
del s

discard() method: used to remove a specified element from a set. If the element is found, it is removed, and if it is not found, no error is raised.

remove() method: which removes the specified element but raises a KeyError if the element is not present in the set.

pop() method: as sets are unordered, there is no concept of the “last” or “first” element. It removes and returns a random element from the set.

# Creating a set
s = {1, 2, 3, 4}
s.pop() # it will random delete any element.

clear() method: used to remove all elements from a set, making it an empty set.

# Creating a set
s = {1, 2, 3, 4, 5}
# Clearing all elements from the set
s.clear()

#Output
set()

Operation Performed on Set Data type:

s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}


# Union(|)
s1 | s2
# Output {1,2,3,4,5,6,7,8}


# Intersection(&)
s1 & s2
# Output {4,5}


# Difference(-)
s1 - s2
# Output {1,2,3}
s2 - s1
# Output {6,7,8}



# Symmetric Difference(^)
s1 ^ s2
# Output {1,2,3,6,7,8}


# Membership Test
1 not in s1
# Output True


# Iteration
for i in s1:
print(i)

# Output 1
2
3
4
5

Set Function:

# len/sum/min/max/sorted
s = {3,1,4,5,2,7}
len(s)
sum(s)
min(s)
max(s)
sorted(s,reverse=True)

#Output
6
22
1
7
{7,5,4,3,2,1}

Other Methods:

1. isdisjoint() method: isdisjoint() method is used to check whether two sets are disjoint, meaning they have no elements in common. If the sets are disjoint, the method returns True; otherwise, it returns False.

# isdisjoint
s1 = {1,2,3,4}
s2 = {7,8,5,6}
s1.isdisjoint(s2)

#Output
True

2. issubset() method: In Python, the issubset() method is used to check whether a set is a subset of another set. A set s1 is considered a subset of set s2 if every element of s1 is also an element of s2 . If s1 is a subset of s2 , then the issubset() method returns True; otherwise, it returns False.

# issubset
s1 = {3,4,5}
s2 = {1,2,3,4,5}
s1.issubset(s2)

#Output
True

3. issuperset() method: The issuperset() method is used to check whether a set is a superset of another set. A set s1 is considered a superset of set s2 if every element of s2 is also an element of s1. If s1 is a superset of s2, then the issuperset() method returns True; otherwise, it returns False.

# issuperset
s1 = {1,2,3,4,5}
s2 = {3,4,5}
s1.issuperset(s2)

#Output
True

4. copy() method: To create a shallow copy of a set. The copy() method returns a new set with the same elements as the original set.

# copy
s1 = {1,2,3}
s2 = s1.copy()
print(s1)
print(s2)

#Output
{1, 2, 3}
{1, 2, 3}

5. frozenset: In Python, a frozenset is an immutable version of a set. While sets are mutable. Frozensets can be used as keys in dictionaries and elements in other sets.

# Creating a frozenset
frozen_set = frozenset([1, 2, 3, 4, 5])
print("Frozenset:", frozen_set)
# Attempting to add an element to the frozenset will result in an error

#Output
Frozenset: frozenset({1, 2, 3, 4, 5})

Set Comprehension:

Set comprehension is a concise way to create sets in Python using a single line of code. It is similar to list comprehension and dictionary comprehension but is specifically used to create sets.

{expression for item in iterable if condition}
# Using set comprehension to create a set of squares
squares_set = {x**2 for x in range(1, 6)}

# Output
{1, 4, 9, 16, 25}

--

--

Rina Mondal

I have an 8 years of experience and I always enjoyed writing articles. If you appreciate my hard work, please follow me, then only I can continue my passion.