What is set in python? And how do we use it?

Set is one of a built-in collection data types in python, the other three are List, Tuple and Dictionary.

Set is used to store multiple data-types item inside single variable, but as told earlier set is a collection data types, so it stores bulk items inside one variable but it has a different properties as compared to other collection data types.


  • Set is collection data type
  • Unindexed
  • No duplicate values
  • Insertion order is not preserved
  • Mutable
  • Heterogenous
  • No indexing or slicing

Set items are unindexed, meaning like in tuple or list where each item has its specific location like 1st item is at 0th position 2nd item is at 1st position, so this is not the case with set, it stores item at any desired place.

Set does not allow duplicate values, meaning all items in set should be unique, even if you try to pass duplicate values inside set it will not accept it also does not throw any error.

Insertion order it not preserved in set. Since set is unindexed collection data type, it does not store the insertion order (The fashion in which each item is passed).

Set is mutable, meaning we can add or delete element.

Set is a heterogenous, meaning it can hold different types of data be it string, int or float.

Set does not allow indexing or slicing. Slicing means extracting items from particular element to certain particular element.

Set constructor:

We use round brackets and set keyword to declare an empty set using set constructor.

What is set in python?

how do we use it?

Set methods or build in methods in set.

Now if you have read our previous articles about list, we have shown each and every built-in methods present inside list, similarly set has its own built-in methods, but these built-in methods are a bit complex as compared to others, so we have written these built-in methods in such a way that it becomes easy for you to understand, the only thing is that you should read each and every line in detail. 

Method Description
add(x) Adds an element inside set at any location
clear() Removes all the elements from the set and return empty set
copy() Returns a copy of the set
difference(x) Return as set which is in A and not in B. Given => A.difference(B) and vice versa
difference_update(X) Removes the common items from set A. Given A.difference_update(B) and vice versa
discard(X) Remove the specified item, if item is not present it does not throw an error
intersection(X,X,..) Returns a set which has common items. We can compare two or more sets together. A.intersection(B,C,D…..)
intersection_update(X, X,..) Removes the items that is not common in any of the sets. Given => A.intersection_update(B)
isdisjoint(X) Returns True if two set does not have any common item
issubset(X) Returns True if one set is subset of another. Given => A.issubset(B)
issuperset(X) Returns whether this set contains another set or not
pop() Deletes the random item from set, also deleted item can be retrieved in another variable
remove(X) Deletes the specified Item
symmetric_difference(X) Returns a set with non-common items
symmetric_difference_update(X) Removes the common item from original set and insert uncommon item into original set
union(X) Return a new set containing both set’s item but does not repeats items
update(X) Updates the original set from non-common items of another set. Given A.update(B). A is updated with B set


Note:- The ‘X’ inside round brackets (X) represents that method will take argument or not.

Waqar Khan

Hello, everyone, I am Waqar Khan. I have done my B.tech in Computer Science. I love to write about Computer Informational blogs. Before write I did proper research on it. Hope you like our blogs. Thank you.

Related Posts

Leave a Reply

Your email address will not be published.