Join Barron Stone for an in-depth discussion in this video Create and combine sets, part of Programming Foundations: Real-World Examples.
- If you have a collection of different objects that you want to store together, and the order of those objects doesn't matter, then they can be organized into a set. A set is a data structure to store an unordered collection of unique objects, meaning that no two objects in the set are identical. I think I'll invite some friends over tonight for an epic evening of board games. Now to figure out who to invite. As a modern man, I keep track of all my friends through social networks, but I keep them organized into different groups.
I have one group for my college buddies, another group for my coworkers, and also a group for my family friends. Each of these three groups of friends represents a set, which is an unordered collection of unique items, in this case, my friends. Representing them in a set is different from a list because there is no ranking or order to the set. I can't tell you who my number one friend is because they are all equally good friends within each set. These three circles contain sets of my friends. The first thing I want to do is combine these three separate sets into a single set containing all of my friends.
If you look closely, you'll notice that I do have some friends in common between the three sets. I have four friends who belong in more than one group of my friend groups. To combine these three sets into a single set containing all of my friends, I can use the .union() operator. Since all of the items in a set must be unique, we lose any duplicates, so that the resulting set will only have one of every item. Let's look at a quick code example to demonstrate creating and combining sets. I'll open up the start_07_01_combining_sets Python script.
In Python, we can create a new set by passing a list of items to the set function. In this example, I've created a list of strings containing the names of my friends for each group, and I've passed those lists to the set function to create the three set objects: college, coworker, and family friends. I'll run the script and switch over to the interactive window to use it. If I type the name of one of my sets, like college, the shell will print out all of the items in that set. I can check the number of items in a set by using the len length function on that set object.
This shows that I have eight college buddies, eight coworkers, and eight family friends. Now I'll combine these sets into a single set containing all of my friends. To do that, I'll pick any one of the sets. Let's use college, and then I call the union method on the set and pass it the other two set objects as arguments, and now we have a single set with all of my friends. Let's take a look at the contents of the friends set. It looks like everybody's here.
The order that the names are displayed is irrelevant because inside of the set, it's like the Wild West. There's no order at all. There is no hierarchy in this combined set that places my buddies in order. The original sets are all intermixed equally in the new set. When I check the size of the friends set, it returns that I have 20 friends total, and this makes sense because I have four friends who belong to two or more of the original sets. I started with eight friends in each of my three friend groups, which makes 24 people, but when I combined those three sets, the union operator removed the duplicate names, leaving me with 20 individual, unique friends in the final set.
Sets are a great data structure to use when you need to keep track of a collection of objects that are all unique and don't have any order. In this example, I used sets to keep track of my friends, but some other common uses for sets in programming would be to keep track of unique words appearing in a document or to organize user accounts in a system.
- Reusing functions
- Local vs. global variables
- Creating and naming custom objects
- Class inheritance
- Modules and packages
- Multidimensional lists and tuples
- Queues and stacks
- Creating and combining sets
- Storing data in dictionaries
- If/else and switch statements
- For vs. while loops
- Error handling
- Polling and event-driven programming