In this video, learn how to use the permutation and combination methods from Python's itertools module to create different sets of data. Kathryn will review the differences between permutations and combinations, and explain why they are helpful in some coding situations.
- [Narrator] We can also calculate permutations and combinations in Python using the itertools module. So starting with permutations, a permutation is a way in which a set or number of things can be ordered or arranged. Permutations take all the items given and reorder them in different ways. So here we have one, two, and three. We can order them as one, two, three, one, three, two, three, two, one, et cetera. So why is this helpful? Let's go back to the code and try an example. Say we have a variable called election and so we have election equals, and we're going to make it equal a dictionary.
We are going to have one and it's going to be associated with Barb. We're going to have two and it's going to be associated with Karen and then we're going to have three, and it's going to be associated with Erin. So for an election we'd want to know what are the possible outcomes for that given election. And so we'll need to import itertools for this because we're going to use the permutations method inside of itertools. And so we're going to go for p in itertools.permutations election and this is saying for each permutation in the election permutations we're going to go ahead and print it out.
This will give us all the possible orderings for the election and so running this we get what we saw on the slide. We have one, two, three, one, three, two, two, one, three, et cetera. Now these numbers don't really make much sense because we really want the name that's associated with them and so to get the name, all we have to do is switch election with election.values. And so to write this again, we'll go for p one in itertools.permutations election.values We'll go ahead and print p one, running this we get all the different orderings.
And so, say this is an election and Barb got the presidency, Karen got the vice presidency and Erin got the secretary. Or you could have Karen got the presidency, the vp, secretary, et cetera. These are all the possible permutations that you can get with Barb, Karen and Erin. Another thing we can calculate with itertools are combinations. However, before going into combinations, let's review what they are. They are kind of like permutations in that they list a set of items but for combinations no set has the exact same elements as another.
So say you have three items, work, eat, and play but you can only do two of those things. The combinations of two that would result would be work eat, work play, and eat play. Notice that we don't have both work eat and eat work because these have the exact same elements and therefore count as the same combination. Now let's try this in code. Say we have a list of colors we can paint with and so we'll have a variable called colors for painting and we'll have a list with red, blue, purple, orange, yellow and pink but when we paint, we only want to paint with two colors and so we can use the combinations method inside the itertools module.
To do this we'll go for c in itertools.combinations colors for painting, which is our data, and then two because we want to choose two items out of this data for the combination. And then we'll print each combination that we get. And so printing this, these are the colors that we could pair for our painting. We could have red and blue, red and purple, red and orange. Notice that you don't see the same combination twice and so because we have red and pink, we're not going to see pink and red.
If we were, then that would be part of a permutation. If we change this two to three here, then we're going to get combinations with three items. And so applying this, here we get three items in our combinations, and again, you're not going to have two entries with the same elements in a different order. In the future, itertools will definitely come in handy when you're trying to rearrange data and see what can happen with the different combinations. It's also really useful if you need to repeat values or cycle through things. Instead of writing a bunch of lines of code you can just call that one method and all the work is done for you.
- Working with logical and comparison operators
- Getting a list of numbers with the range() and list() functions
- Using mathematical functions such as round(), abs(), and pow()
- Calculating a given input's length
- Importing and using the math module
- Reading a user's command-line arguments
- Getting the current time
- Formatting dates and times with datetime
- Creating a timer
- Using urllib to get content from the Internet
- Using the JSON module to decode content