Get startedGet started for free

Set theory

1. Set theory

In this lesson, we'll continue to use the Pokémon dataset to enhance our skills when it comes to comparing objects. Let's dive in.

2. Set theory

Often, we'd like to compare two objects to observe similarities and differences between their contents. When doing this type of comparison, it's best to leverage a branch of mathematics called set theory. As you know, Python comes with a built-in set data type. Sets come with some handy methods we can use for comparing. We'll explore each of these methods later on. The main takeaway is that when we'd like to compare objects multiple times and in different ways, we should consider storing our data in sets to leverage these elegant and efficient methods. Another nice feature of Python sets is their ability to quickly check if a value exists within its members. We call this membership testing. In this lesson, we'll show that using the in operator with a set is much faster than using it with a list or tuple. Let's explore a few examples.

3. Comparing objects with loops

Suppose we had two lists of Pokémon: list_a and list_b.

4. Comparing objects with loops

We'd like to compare these lists to see which Pokémon appear in both lists.

5. Comparing objects with loops

We could use a nested for loop to compare each item in list_a to each item in list_b and collect only those items that appear in both lists. But, iterating over each item in both lists is extremely inefficient.

6. Comparing objects with set theory

Instead, we should use Python's set data type to compare these lists. By converting each list into a set, we can use the dot-intersection method to collect the Pokémon shared between the two sets. One simple line of code and no need for a loop!

7. Efficiency gained with set theory

When comparing runtimes, we see that using sets is a much faster approach.

8. Set method: difference

We can also use a set method to see Pokémon that exist in one set but not in another. To gather Pokémon that exist in set_a but not in set_b, use set_a-dot-difference(set_b).

9. Set method: difference

If we want the Pokémon in set_b, but not in set_a, we use set_b-dot-difference(set_a).

10. Set method: symmetric difference

To collect Pokémon that exist in exactly one of the sets (but not both), we can use a method called the symmetric difference.

11. Set method: union

Finally, we can combine these sets using the dot-union method. This collects all of the unique Pokémon that appear in either or both sets.

12. Membership testing with sets

Another nice efficiency gain when using sets is the ability to quickly check if a specific item is a member of a set's elements. Consider our collection of 720 Pokémon names stored as a list, tuple, and set.

13. Membership testing with sets

We want to check whether or not the character, Zubat, is in each of these data structures.

14. Membership testing with sets

When comparing runtimes, it's clear that membership testing with a set is significantly faster than a list or a tuple.

15. Uniques with sets

One final efficiency gain when using sets comes from the definition of set itself. A set is defined as a collection of distinct elements. Thus, we can use a set to collect unique items from an existing object. Let's revisit the primary_types list, which contains the primary types of each Pokémon. If we wanted to collect the unique Pokémon types within this list, we could write a for loop to iterate over the list, and only append the Pokémon types that haven't already been added to the unique_types list.

16. Uniques with sets

Using a set makes this much easier. All we have to do is convert the primary_types list into a set, and we have our solution: a set of distinct Pokémon types.

17. Let's practice set theory!

Let's practice using set theory with a few coding examples.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.