Home / Blog / Python Sets | Operations and Examples
sets in python

Python Sets | Operations and Examples

The uniqueness of Sets is that sets are unordered means sets cannot support indexing Python will store the set’s items at random locations and store unique elements means when we try to store duplicate items Python will merge them.

Example

set_a = {"red", "yellow", "orange"}
print(set_a)
Python
Output:
{‘red’, ‘yellow’, ‘orange’}

Access Item

We cannot access items from sets by using index values because as we discussed above sets do not support indexing but we can assess the set’s values by using loops and keywords.

Change Item

After creating a set, we cannot change its items, but we can add new items.

Add one item by using add () method.

Example

set_a = {"red", "yellow", "orange"}
set_a.add("Banana")
print(set_a)
Python
Output:
{‘orange’, ‘red’, ‘yellow’, ‘Banana’}

We add multiple items by using update () method.

Example

set_a = {"red", "yellow", "orange"}
set_a.update(["Pink", "White", "Blue"])
print(set_a)
Python
Output:
{‘Blue’, ‘red’, ‘orange’, ‘White’, ‘Pink’, ‘yellow’}

Print a Set using for Loop

We can also use for loop to print set items in one go except printing one by one.

set_a = {"red", "yellow", "orange"}
for x in set_a:
    print(x)
Python
Output:
red
yellow
orange

Print Length of Set

print number of total Items in the set using len () method:

set_a = {"red", "yellow", "orange"}
for x in set_a:
    print(len(x))
Python
Output:
3
6
6

Remove item from Set

Use remove () or discard () method to remove an item from set.

Example

Remove “Yellow” by using remove () method:

set_a = {"red", "yellow", "orange"}
set_a.remove("yellow")
print(set_a)
Python
Output:
{‘red’, ‘orange’}

Note: When we try to remove an item which is not exist in the set by using remove () method then, remove () will show an error.

Example

Remove “Yellow” by using discard () method:

set_a = {"red", "yellow", "orange"}
set_a.discard("yellow")
print(set_a)
Python
Output:
{‘red’, ‘orange’}

Note: When we try to remove an item which is not exist in the set by using discard () method then, discard () will show an error. 

Pop item from Set

We can use pop () to remove an item from the set pop method removes the last Item from usually and it will return the removed value.

set_a = {"red", "yellow", "orange"}
x = set_a.pop()
print(x)
Python
Output:
red

Note: Sets are unordered so, we have no idea about what item gets removed.

Delete item from Dictionary

The method del () will remove the whole set:

set_a = {"red", "yellow", "orange"}
del set_a  # This will delete the set_a variable
print(set_a
Python
Output:
Traceback (most recent call last):
File “”, line 3, in
NameError: name ‘set_a’ is not defined

Clear items from Set

The clear () method return an empty set:

set_a = {"red", "yellow", "orange"}
set_a.clear()
print(set_a)
Python
Output:
set()

This code clears all elements from the set set_a using the clear() method, resulting in an empty set, which is denoted by {}.

Join Two Sets

In Python there are many methods to join sets.

We can use union () method to join two sets it will return a new set containing all the items from both sets same as we calculate a union of two sets in mathematics.

Example

set1 = {"one", "two", "three"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
Python
Output:
{1, 2, 3, ‘two’, ‘three’, ‘one’}

The update () method will insert all the item of set2 into set1:

Example

set1 = {"one", "two", "three"}
set2 = {1, 2, 3}
set1.update(set2)
print(set1)
Python
Output:
{1, 2, 3, ‘three’, ‘two’, ‘one’}

This code updates set1 by adding all elements from set2 using the update() method. The resulting set set1 contains all unique elements from both set1 and set2.

Create Set by using Constructor

We can also create a Set by using built in constructor Set ():

set_b = set(("red", "yellow", "orange"))
print(set_b)
Python
Output:
{‘red’, ‘yellow’, ‘orange’}

This code creates a set set_b containing the elements “red”, “yellow”, and “orange” using the set constructor, and then prints the set.

Author: TCF Editorial
Copyright The Cloudflare.

Related Articles:

How to Install Python on Windows | Introduction to Python
A Beginner’s Guide To Python Variables and Types
Python Operators | Types of Operators in Python
Basic Python Syntax | First Program in Python
Python Arrays | List Methods
Dictionaries in Python: A Comprehensive Tutorial

Tuple in Python (with Example)

Post navigation