Anyone Can Code.



Jyotirmay Zamre

This tutorial will be the complete discussion of Sets for this course. It's quite a long one so take your time and make sure you understand everything.

A set is a collection of data which is unordered and unindexed. Their contents are enclosed within curly brackets, but they are not dictionaries.

    names = {'John', 'Henry', 'Julia', 'Paul'}
{'Julia', 'John', 'Paul', 'Henry'}

Since sets are unordered, the order in which the items are placed can change at any given point in time. Therefore, there are no fixed positions and so one can't access the items using an index value. However, one can still loop through the items in the set using a for> loop.

    names = {'John', 'Henry', 'Julia', 'Paul'}
    for x in names:
Julia John Paul Henry

You can check whether a specific item is present in the set by using the in keyword.

    names = {'John', 'Henry', 'Julia', 'Paul'}
    if 'John' in names:

You cannot change the items within a created set but you can add new items or remove items. However, the added item will only be added if it doesn't already exist in the set.

    #the add() method is used to add one item
    names = {'John', 'Henry', 'Julia', 'Paul'}

    #the update() method is used to add multiple items
    names = {'John', 'Henry', 'Julia', 'Paul'}
    names.update(['Candace', 'Kenan', 'Joe'])
{'Julia', 'Paul', 'Henry', 'John', 'Candace'} {'Julia', 'Joe', 'Paul', 'Henry', 'Kenan', 'John', 'Candace'}

There are many ways to join two or more sets in Python. The union() method creates a new set which contains items from both sets and the update() method transfers all the elements from one set to the second.

    #union() method
    set1 = {'John', 'Henry', 'Julia', 'Paul'}
    set2 = {3, 19, 5, 2}
    set3 = set1.union(set2)

    #update() method
    set1 = {'John', 'Henry', 'Julia', 'Paul'}
    set2 = {3, 2, 7, 5}
{'Julia', 2, 3, 'Paul', 5, 'Henry', 19, 'John'} {'Julia', 2, 3, 'Paul', 5, 7, 'Henry', 'John'}

Set Theory

Before we look at some more Set methods, let's look at some basic Set Theory. If you are already familiar with this, scroll down.

In Python we have some set methods that are similar to mathematical set operations. In this tutorial, we are looking at union, intersection, subsets, supersets and difference of sets.

Union and Intersection

Union is the combination of two or more sets, without repeating common elements. An intersection is the set of all common elements between one or more sets.

For example, if we have A = {2,3,4} and B = {3,5,6}, then A ∪ B = {2,3,4,5,6} and A ∩ B = {3}

Subsets and Supersets

A subset is a set which contains some of the elements of another set. A superset of another set is a set that contains the elements of the second set

If we have a set A = {2,3,4} and B = {2,3,4,5,6}, then A is a subset of B and B is a superset of A

Difference of Sets

The difference of two sets is the elements of one set minus the elements in common with another set. For example, if we have A = {2,3,4} and B = {3,5,6}, then A-B = {2,4}

Set Methods

  1. len()

    the len() method determines the number of items in a set

        names = {'John', 'Henry', 'Julia', 'Paul'}

  2. remove()

    the remove() method removes a specified item from the set

        names = {'John', 'Henry', 'Julia', 'Paul'}
    {'Julia', 'Paul', 'Henry'}

  3. pop()

    the pop() method removes the last item in the set so you will not know what item gets removed

        names = {'John', 'Henry', 'Julia', 'Paul'}
    {'John', 'Paul', 'Henry'}

  4. clear()

    the clear() method empties the set

        names = {'John', 'Henry', 'Julia', 'Paul'}

  5. intersection()

    the intersection() method returns a set that is the intersection of two other sets

        set1 = {'John', 'Henry', 'Julia', 'Paul', 19}
        set2 = {3, 19, 5, 2, 'Henry'}
        set3 = set1.intersection(set2)
    {19, 'Henry'}

  6. issubset()

    the issubset() method checks whether another set contains a particular set or not. It returns a boolean.

        set1 = {'John', 'Paul'}
        set2 = {'John', 'Henry', 'Julia', 'Paul'}

  7. issuperset()

    the issuperset() method checks whether another set is a superset of a particular set or not

        set1 = {'John', 'Henry', 'Julia', 'Paul'}
        set2 = {'John', 'Paul'}

  8. difference()

    the difference() method returns a set that contains the item only present in the first set.

        set1 = {'John', 'Henry', 'Julia', 'Paul'}
        set2 = {'John', 45, 'Alex'}
    {'Julia', 'Paul', 'Henry'}

That's it for this tutorial! Click Next to access the next tutorial!

❮ Back

Next ❯

Copyright © 2021, Q-Programming | Created by Arjun Sodhi, Balpreet Juneja and Sammarth Kumar | Developed and Designed by Sammarth Kumar