Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Python Set: Tutorial for Python Beginners [Video]

DZone's Guide to

Python Set: Tutorial for Python Beginners [Video]

This tutorial is geared towards newcomers to Python and goes over an important data structure: sets. Let's dive in along with some code.

· Open Source Zone ·
Free Resource

Are you looking for ways to manage your open source risk? We can help. Learn more.

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 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.

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 exist 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. Let's 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.

Conclusion

There is more to Python sets but I covered the basics here to get you started. For more information check out 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.

Software composition Analysis for DevSecOps. Start finding vulnerabilities in your open source components today.

Topics:
python ,open source ,web dev ,sets ,data structures ,code

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}