Python Tutorial : Sets for unordered and unique data

Python sets store unique, unordered elements and automatically remove duplicates. Learn to add and remove items, then use union, intersection, and difference operations to compare datasets like cookie collections for quick year-over-year analysis.
Chapters
Now that you've learned about lists and tuples, let's look at our last built in array data type. The set sets are excellent for finding all the unique values in a column of your data, a list of elements, or even rows from a file. We use sets when we want to store unique data elements in an unordered fashion. For example, I might want to store a list of each cookie type I had without any duplicates. Sets are also mutable, so I can add or remove elements from them. We're just going to scratch the surface of what can be done with a set. It has many more capabilities that align with set theory for mathematics. A set is almost always created from a list. For example, I might have a list of cookies I've eaten today. I can make a set of them by passing them into the set constructor. Finally, if I print it, you might notice that although I had three chocolate chip cookies in my set, once I made it a set, there is only one occurrence of it in that set. This occurs because sets only store unique items. Now let's explore modifying a set. When working with a set, we will use the add method to add a new element to the set. It will only add the element if it's unique. Otherwise it just continues on. Also, we can add multiple items using the update method. The update method takes a list of items and adds each one to the set if it's not already present. While making the first two sections of this chapter, I ate two more cookies, a biscotti and a chocolate chip cookie. So let's use the add method to add those to our set and to print the result. Finally, Hugo had some cookies. So let's use the update method to add the cookies he ate to our set and print them. Now let's learn how to remove some elements from our set. When removing data from a set, we can use the discard method to safely remove an element from the set by its value. No error will be thrown if the value is not found. We can also use the POP method to remove and return an arbitrary element from the set. Let's remove with the discard method the biscotti. As Hugo and I had some debate about whether or not this was even a cookie, we'll then print the set. Next, we'll pop two cookies out of the list to decide what we could eat next. Finally, we're going to leverage some of that set theory for math to perform some very quick comparison operations. The union method on a set accepts a set as an argument and returns all of the unique elements from both sets as a new one. The intersection method also accepts a set and returns the overlapping elements found in both sets. This is great for comparing data year over year. For example, speaking of examples, I'm going to create two new sets of the cookies that Hugo and I have eaten. Then I'm going to use the Union method to see the full set of cookies eaten by both of us. Finally, I used the intersection method to see the cookies that Hugo and I both ate in common. While these two methods help us find commonality, sets also provide methods to help us find differences. We can use the Difference method, which accepts a set to find elements in one set that are not present in another set. The target we call the difference method on is important as it will be the basis for our differences. So here I want to see the cookies that I ate that Hugo didn't, which I can do by calling difference on my set of cookies and giving it hugoset. I can perform the reverse of this operation by using Hugo's set as the target.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







