Set in Python

In this Python tutorial, we will learn about sets. Here we will discuss, how to create, update, delete and other operations performed on set in Python.

What is a Set

  • A set is an unordered collection of objects.
  • A set is an unindexed collection in Python.
  • Each element in the set is unique.
  • Set is mutable, we can add or remove items.

How to Create a Set

In Python, sets are created by placing all items inside the curly { } brackets, separated by a comma. We can also create a set by using the built-in function set(). It can have any numbers of a different type as elements. But mutable elements like list, a dictionary can not be used as an element in the set.

#create an empty set

#set with integers
n={11, 22, 33}

#set with text value
n={"red", "green","yello"}

#set with mixed values
n={11, 22, "Red", (9, 8, 7)}

#set does not contain duplicate values
n={1, 11, 3, 1, 22, 11}

#output : print unique value, remove duplicate

Access Items of the Set

We can not access an individual item of the set, because sets are unordered and unindexed. We can loop through the sets using for loop.

#creating set
num={11, 22, 33}

#print entire set

#loop through set
for n in num:

Add Elements to the Set

As we know a set is a mutable object, so we can add one or multiple items to the set.

  • add() – To add a new item to the set.
  • update() – To add multiple items to the set.
#create a set
color={"Red", "Green", "Yellow"}
#add one item
#output: {'Yellow', 'Red', 'Blue', 'Green'}


#create a set
color={"Red", "Green", "Yellow"}
#add multiple items
#output: {'Green', 'Yellow', 'Pink', 'Blue', 'Red'}

Delete Elements from the Set

Python has del keywords and many built-in functions to remove an item from the set.

  • remove() – Remove given item from the set, return an error if the item is not in the set.
  • discard() – Remove given item from the set, it does not raise an error if the item is not in the set.
  • pop() – It removes and return removed item from the set.
  • clear() – Remove all items from the set.
  • del – The del keyword remove complete set.
#remove Green from the set
color={"Red", "Green", "Yellow"}

#delete Yellow from the set using discard()
color={"Red", "Green", "Yellow"}

#remove any random item from the set
color={"Red", "Green", "Yellow"}

#empty the set
color={"Red", "Green", "Yellow"}

#delete complete set
color={"Red", "Green", "Yellow"}
del color
print(color) #output: error

Set Operations in Python

The set in Python is defined as set in Mathematics. Various set operations like union, intersection etc can possible. Each of these operations has a corresponding method and operators.

Union of Sets

Joining of two sets (elements from two sets) using union() method or vertical bar(|).

s1={1, 2, 3, 4, 5}  #set1
s2={3, 5, 8, 9}     #set2

#using union operator
print(s1 | s2)

#using union() method

#output: {1, 2, 3, 4, 5, 8, 9}

Intersection of Sets

To get a common item (intersection) of two sets in Python, using intersection() method or using & symbol.

s1={1, 2, 3, 4, 5}  #set1
s2={3, 5, 8, 9}     #set2
#using & operator
print(s1 & s2)

#using intersection() method

#output: {3, 5}

Difference of Sets

Return a set containing the difference (element from first but not in the second set) using difference() method or minus (-) symbol.

s1={1, 2, 3, 4, 5}  #set1
s2={3, 5, 8, 9}     #set2
#using - operator
print(s1 - s2)

#using difference() method

#output: {1, 2, 4}

Symmetric Difference of Sets

Return a set with elements in either first or second set using symmetric_difference() method or ^ symbol.

#-----------symmetric difference------------------
s1={1, 2, 3, 4, 5}  #set1
s2={3, 5, 8, 9}     #set2
#using ^ operator
print(s1 ^ s2)

#using symmetric_difference() method

#output: {1, 2, 4, 8, 9}

Created on By S. Shekhar

Python – Quiz 16

1 / 4

n={1, 11, 3, 1, 22, 11} is a set?

2 / 4

We can access elements of a set.

3 / 4

We cannot change elements of a set.

4 / 4

a^b is?

Your score is

The average score is 55%