Python Sets: Add, Update, Delete and Access Items

We all are familiar with the word ‘set’. It is a common mathematical term. It is known as set theory and represents a collection object. In Python, the set has the same relative meaning. It is an unordered collection of items that are unindexed and unordered. In Python, a collection object should be treated as a set if it has three rules. It must be identified with curly brackets. All items must be unique. All items must be immutable. We also know that a set itself is mutable and hence add or remove items are not permitted here. In Python, the utility of sets is the same as mathematical set operations. We can do union, intersection, symmetric difference in Python as well. An example of a set is given below,

myset = {10,20,50,100}
print(myset)

# Output
# set([100, 10, 20, 50])

Sets are unordered so the input sequence and the output sequence will not be the same.

Access Set Items

Set can't access the items by using index numbers. But we can print all the items of the set through for loop and check an element is present or not by using the membership keyword ‘in’. We cannot change items of the set after the creation. Examples are given below,

myset = {100,200,500,1000}

for x in myset:
    print(x)

print(500 in myset) 

# Output
# true

Add or Update in Set

We can add one item to a set by using the add() method and more than one item to a set by using the update() method. Examples are given below

myset = {100,200,500,1000}
myset.add(2000)
print(myset) 

# Output
# set([200, 500, 100, 1000, 2000])

myset.update([4000,5000,6000])
print(myset) 

# Output
# set([4000, 6000, 100, 200, 2000, 1000, 5000, 500])

Length of a Set

We can get the length of a set by using the len() method. It will compute the number of items of that set. An example is given below

myset = {20,70,50,100,120}

print(len(myset)) 

# Output
# 5

Remove Item from Set

By using the remove(), and discard() method, we can remove items from a set. If the item does not exist in the set, the remove method will give an error. But for the discard method, it will not give any error. We can use the pop() method for removing an item from a set. But it only removes the last element of a set. Again we know that sets are unordered so programmers may not know which item will be deleted.  Examples are given below

myset = {20,70,50,100,120}
myset.remove(70)
print(myset) 

# Output
# set([120, 100, 50, 20])

myset.remove(200)
print(myset) 

# Output
# error

myset.discard(20)
print(myset) 

# Output
# set([120, 100, 50])

myset.discard(220)
print(myset) 

# Output
# set([120, 100, 50]) (no error)

myset.pop()
print(myset) 

# Output
# set([100, 50])

del Keyword and clear() Method

The clear() method is used to remove all the elements from a set. To completely delete the sets, we have to use the del keyword. Examples are given below,

myset1 = {20,70,50}
myset1.clear()
print(myset1) 

# Output
# set([])

myset2 = {20,100,120}
del myset2
print(myset) 

# Output
# it will give an error

Join Two Sets

We can join two sets of various methods. Those are union() and update() method. In the union method, we have to create a new set and store the elements of two sets. In the update method, items of one set will be inserted into another set. Examples are given below,

myset1 = {20,70,50}
myset2 = {20,100,120}
myset3=myset1.union(myset2)
print(myset3) 

# Output
# set([50, 20, 70, 120, 100])

myset1.update(myset2)
print(myset1) 

# Output
# set([50, 20, 70, 120, 100])

Set Constructor

Python also supports set constructor. To make a set, we can use a set constructor. An example is given below,

myset = set(("John", "Devid", "Queen")) # this round-brackets denote the set constuctor

print(myset) 

# Output
# set(['Devid', 'John', 'Queen'])

That’s all about the Python set; we will discuss on Python Dictionary in the next tutorial.