Python Sets

    In this tutorial, we will discuss everything of Python Sets. We will learn how to create and remove a set and also learn all the properties and operation associated to a set.

    Python Sets

    A set is an unordered, and unique collection of Items. A set does not store duplicates items. Sets are considered as mutable data objects because its items can be deleted and added.

    Hot to create a Set?

    To create a set we use curly braces { } and store each item inside it and separate them with a comma.  Like a list, a set can also store any type of data object except the mutable items. We can also convert a list into a set using a built-in function set() Let’s understand it with an example:

    set_1 = {1,2,"hello","world"}
    set_2 = {True , False, (1,2,3,4) }


    {1, 2, 'hello', 'world'}
    {False, True, (1, 2, 3, 4)}

    Behind the Code: In the output statement of set_2, the output is not the same as we have assigned it because sets are unordered collections of items. The item inside the sets shuffle.

    Change Sets items

    Sets are mutable and by using add() and update() methods we can add single and multiple items in the set. Sets store items in unordered sequences so there is no concept of accessing Sets item using index. Set also do not store duplicate items so if we try to add an item which is already in a set it won’t store. Examples

    set_1= {1,2,3,4,5}
    set_1.add(6)      #adding an item in a set
    set_1.update({7,8,9}) #addind multiple items
    set_1.update({2,2,2,2,3,3})          #adding duplicate items


    {1, 2, 3, 4, 5, 6, 7, 8, 9}

    Remove items from a set

    We can remove the specific items from a set using the remove() and discard() methods. The difference between remove() and discard method is, the remove() method will raise an error if we try to remove that item which is not present in the set, that’s why with sets we use discard method. Example:

    set_1= {1,2,3,4,5}
    {1, 3, 5}

    We can also use pop() method to remove the last element of the set though there is no sequence in a set, so it pops out the random element. And to clear all items of a set we use the clear() method. Example

    set_1 = {1,2,3,4,5,6,7,8}
    set_1.clear()      #Clear all items of set



    Set Operations

    Like the mathematical concept of sets, python set also follows all the mathematical set operations such as union, intersection, difference and symmetric difference.

    Union (|)

    We use | operator and union() method to perform union operation between two sets. Union is used to combine two set without duplicating any items A | B == B|A Example:

    a = {1,2,3,4}
    b = {3,4,5,6}
    c= a | b                 #union
    d = a.union(b)


    {1, 2, 3, 4, 5, 6}
    {1, 2, 3, 4, 5, 6}


    We use & operator and intersection() method to perform intersection operation between two sets. We use the intersection operation to get the common items between two sets. Example:

    a = {1,2,3,4}
    b = {3,4,5,6}
    c = a & b
    d = a.intersection(b)


    {3, 4}
    {3, 4}

    Set Difference:

    We use – operator and difference() method to perform set difference operation. A -  B return only those elements of A which are not in B. A – B != B – A Example

    a = {1,2,3,4}
    b = {3,4,5,6}
    c = a - b
    d = b - a
    print("a-b is:", c)
    print("b-a is:",d)


    a-b is: {1, 2}
    b-a is: {5, 6}

    Set Symmetric Difference

    Symmetric difference sets of A and B will return a set of elements having elements of both sets except the common in both. We can use ^ operator and symmetric_difference() method to perform symmetry difference. A ^ B == B ^ A Example:

    a = {1,2,3,4}
    b = {3,4,5,6}
    c = a ^ b
    d = b ^ a
    print("a ^ b is:", c)
    print("b ^ a is:",d)


    a ^ b is: {1, 2, 5, 6}
    b ^ a is: {1, 2, 5, 6}

    Python Set Methods:

    Python has its own methods here we have mentioned all the sets methods:

    • add()
    • clear()
    • copy()
    • difference()
    • difference_update()
    • discard()
    • intersection()
    • intersection_update()
    • isdisjoint()
    • issubset()
    • issuperset()
    • pop()
    • remove()
    • symmetric_difference()
    • symmetric_difference_update()
    • union()
    • update()


    Frozensets are those sets which are immutable and to form a immutable set or frozensets we use frozenset() function. Example:

    fs = frozenset([1,2,3,4,5,6,7])
    fs_2 = frozenset ( ["hello", "world"])
    print("type of fs is :", type(fs))
    print("fs_2 is:", fs_2)


    type of fs is : <class 'frozenset'>
    fs_2 is: frozenset({'world', 'hello'})