SET Data Structure in PYTHON

SET Data Structure in PYTHON

The SET Data Structure in PYTHON is an unordered collection of data type that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements.

If we want to represent a group of values without duplicates where order is not important then we should go for set Data Type.

  1. If we want to represent a group of unique values as a single entity, then we should go for SET Data Structure in PYTHON.
  2. Duplicates are not allowed. v Insertion order is not preserved. But we can sort the elements.
  3. Indexing and slicing not allowed for the set.
  4. Heterogeneous elements are allowed.
  5. Set objects are mutable i.e once we create set object we can perform any changes in that object based on our requirement.
  6. We can represent set elements within curly braces and with comma separation
  7. We can apply mathematical operations like union, intersection, difference etc on set objects.
  8. set is growable in nature, based on our requirement we can increase or decrease the size.

For Example:

Set = {4,3,6.6,”Hello”}


Output: {‘Hello’, 3, 4, 6.6}


While creating empty set we have to take special care.  Compulsory we should use set() function.  s = {} à It is treated as dictionary but not empty set.

Important Functions of Set:

1) add(x):

Adds item x to the set.

1) s={10,20,30}

2) s.add(40);

3) print(s) #{40, 10, 20, 30}

2) update(x,y,z):

To add multiple items to the set.

Arguments are not individual elements, and these are Iterable objects like List, Range etc.

All elements present in the given Iterable objects will be added to the set.

1) s={10,20,30}

2) l=[40,50,60,10]

3) s.update(l,range(5))

4) print(s)


Output: {0, 1, 2, 3, 4, 40, 10, 50, 20, 60, 30}


We can use add() to add individual item to the Set,where as we can use update() function to add multiple items to Set.

add() function can take only one argument where as update() function can take any number of arguments but all arguments should be iterable objects.

3) copy():

Returns copy of the set.  It is cloned object.

1) s = {10,20,30}

2) s1 = s.copy()

3) print(s1)


4) pop():

It removes and returns some random element from the set.

1) s={40,10,30,20}

2) print(s)

3) print(s.pop())

4) print(s)



{40, 10, 20, 30}


{10, 20, 30}


5) remove(x):

It removes specified element from the set.

If the specified element not present in the Set then we will get KeyError.

1) s = {40, 10, 30, 20}

2) s.remove(30)

3) print {à(s) 40, 10, 20}

4) s.remove(50 KeyError: à) 50

6) discard(x):

1) It removes the specified element from the set.

2) If the specified elements are not present in the set then we won’t get any error.

1) s = {10, 20, 30}

2) s.discard(10)


7) clear():

To remove all elements from the Set.

1) s={10,20,30}

2) print(s)

3) s.clear()

4) print(s)



{10, 20, 30}


This is all about the SET Data Structure in PYTHON, hope you liked it.

Related Posts:

Python Tutorial – Learn Python

What is Python? What makes Python so Powerful?

Variables in Python – Constant, Global & Static Variables

Namespacing and Scopes in Python

Operators in Python

Data Types in Python

STRING Data Type in Python

LIST Data Structure in PYTHON

TUPLE Data Structure in PYTHON

Differences between List and Tuple


Database Programming in Python

What is Multithreading in Python?

Python Exception Handling Using try, except and finally statement

File Handling in Python

Python Random module

Python reduce() Function

Python map() Function

Python filter() Function

Lambda Function in PYTHON (Anonymous Function)

Python Interview Question And Answers