top of page

Set in Python

Updated: Apr 19, 2023

The set data type is supported in Python.




There are only distinct elements in a set.



a = {"new", "old", 1, 2}
print(a)

print(len(a))


The set() constructor can be used to initialise a set.



b = set((1,2,3))
print(b)

All the elements can be traversed in a loop of a set.


for x in b:
    print(x)
    
b.add(10)
print(b)

The update method can be used to add new elements to a set.


c = [89, 98]
b.update(c)
print(b)

The remove method can be used to remove elements from a set.


b.remove(10)
print(b)


The pop method is used to remove the last element in a set.


b.pop()
print(b)

The clear method removes all the elements in a set.


b.clear()
print(b)

The del keyword deletes a set.


del b

The link to the Github repository is here .


Subscribe to get all the updates

© 2025 Metric Coders. All Rights Reserved

bottom of page