Python Set: Tutorial for Python Beginners

Python Set: Tutorial for Python Beginners

(Last Updated On: September 22, 2018)

In this post I will go over what a Python set is and how to use them in your Python programs. With sets we can manage data just like math sets.

Python sets let us work with sets of values much like we do in math.

Video Tutorial

Creating a Set

You create a set using the curly brackets:  {}  much like when you declare a dictionary.

The difference is that sets don’t have a key value like dictionaries do.

For example, we will create a set with numbers from 0 to 3:

mySet = {0, 1, 2, 3}

Sets have unique values.  So if you declare a set with duplicate values, you will end up with a set that only contains the unique values:

In [1]: mySet = {1,2,3,4}

In [2]: mySet
Out[2]: {1, 2, 3, 4}

In [3]: badSet = {1,1,2,3}

In [4]: badSet
Out[4]: {1, 2, 3}

In [5]: 

As you can see, when I initialized badSet it had duplicate values (the number 1 was listed twice) but when I show the value of the set it only showed the unique values 1, 2, and 3.

We can even create sets from Strings.

In [8]: nameSet = {"Bob", "Frank", "Jill", "Jack"}

In [9]: nameSet
Out[9]: {'Bob', 'Frank', 'Jack', 'Jill'}

Set Ordering

One thing to note is that the ordering of the values is not important.

In [7]: mySet
Out[7]: {1, 2, 3, 6, 8, 10}

This works for strings as well as you may have noticed from the nameSet example above.

In [8]: nameSet = {"Bob", "Frank", "Jill", "Jack"}

In [9]: nameSet
Out[9]: {'Bob', 'Frank', 'Jack', 'Jill'}

Just because the Python set displays the list ordered doesn’t mean if you iterate over it you will get the ordering.

In [11]: for name in nameSet:
    ...:     print("Student: {}".format(name))
    ...:     
Student: Frank
Student: Bob
Student: Jill
Student: Jack

This is because with sets we only care that the value exists and nothing else.

Converting other data types

We can convert other data types to a set using  set() .

For example, we can convert a list to a set.

In [14]: myList = ["Paladin","Roque","Warrior"]

In [15]: mySet = set(myList)

In [16]: mySet
Out[16]: {'Paladin', 'Roque', 'Warrior'}

or a dictionary:

In [17]: myDict = {"class": "Paladin", "name": "Belkas", "server": "Malfurion"}

In [18]: mySet = set(myDict)

In [19]: mySet
Out[19]: {'class', 'name', 'server'}

Notice that the our Python set only contains the key values of our dictionary.

Set Theory

In this section, we use sets like we learned in math class.

Python Set Theory

In this section we are going to use these sets.

In [27]: primeNums = {1, 2, 3, 5, 7, 11, 13, 17, 19}

In [28]: myNums = set(range(20))

In [29]: myNums
Out[29]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}

We can combine sets in several ways.

We can see what values exists in both sets using the ampersand ‘&’ operator.  This is called the intersection of the two sets.

In [30]: primeNums & myNums
Out[30]: {1, 2, 3, 5, 7, 11, 13, 17, 19}

We can see what values that exist in either set called the union of the two sets using the bar ‘|’ or the union() python function.

In [31]: primeNums | myNums
Out[31]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}

In [32]: primeNums.union(myNums)
Out[32]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}

We can see what values exist in the first set but don’t exist in the second set with the difference operator ‘-‘

In [33]: primeNums - myNums
Out[33]: set()

This gives us an empty set because all the values in primeNums exists in myNums.  Lets turn it around.

In [36]: myNums - primeNums
Out[36]: {0, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18}

This gives us a set of numbers from myNums that are not prime numbers.

Need more Python training?

Checkout these awesome Python courses on Pluralsight.  Pluralsight has great video training courses at a great price.  I use Pluralsight every time I need to learn something new.

Conclusion

There is more to Python sets but I covered the basics here to get you started.  For more information checkout the Python Reference.

Be sure to checkout other great Python articles on AdminTome Blog.

If you liked this post then please share it and comment below.  I would love to hear from you.

1 thought on “Python Set: Tutorial for Python Beginners

Leave a Comment

you're currently offline