Want to learn more? Take the full course at https://learn.datacamp.com/courses/da... at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
Now that you've learned about lists and tuples let's look at our last builtin 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 type of cookie I had without any duplicates. Set are also mutable so I can add and 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 from math.
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 list, once I make it a set, there is only one occurrence of it in the 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 is 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 is not 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 print the result. Finally, Hugo also 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 the biscotti from our cookie set as Hugo and I had some debate about whether or not this was even a cookie with the discard method, and print the set. Next, we'll pop two cookies out of the list to decide what we could eat next. Next, we're going to leverage some of that set theory from math to perform very quick comparison operations.
The union method on a set accepts a set as an argument and returns all 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 cookies 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 use the intersection method to see the cookies that Hugo and I both ate. 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 that 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 Hugo's set and I can perform the reverse of this operation by using Hugo's list as the target.
#DataCamp #PythonTutorial #DataTypesforDataScienceinPython