#100DaysOfCode in Python Transcripts
Chapter: Days 19-21: Iteration with itertools
Lecture: Combinations and Permutations

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Next up we're going to look at itertools combinations and permutations. Now let's look at combinations first. Now combinations allows you to get
0:13 the possible combinations of an iterable of the certain, you know, of a certain string or a certain list, okay? So let's just get our setup here.
0:26 From itertools import permutations, combinations, okay? Let's say we have Mike, Bob and myself. All right and we'll make a friends list,
0:41 'cause we're friends right? Please say we're friends. Mike, Bob, and Julian, and we'll split that, not splut, we'll split, okay so we have friends.
0:52 We have this list, Mike, Bob, and Julian. Now with combinations, we can see how many combinations you'll get of the three of us, okay.
1:03 Now this will actually give us a generator, so we're going to use list, we're going to force it to be a list okay, so just bear with me here.
1:15 So print(list(combinations())), okay so this is now we're talking edit tool stuff and in the brackets we have the iterable, okay?
1:25 And the Iris pretty much what we want the combinations to include how many combinations we want. So for example if we specified Iris two,
1:35 okay it would say, okay combinations of two. So Mike and Bob, Bob and Julian, Julian and Mike and so on, okay?
1:42 So we're going to actually choose our friends list, all right? And then we're going to have a length of 2 okay? Let's close all this off.
1:53 And you'll see that we get, well first of all we'll get return, it returns tuples, or tuples. And look at the combination set that we get there.
2:02 We get Mike and Bob, get Mike and Julian, and then we get Bob and Julian. And what is it that you've probably noticed? There's no order, okay.
2:15 There's no, what if the order mattered? If you were trying to return this list but you don't like Mike being first every time,
2:20 what happens if you want it to return a tuple as Bob, then Mike, Julian, then Mike, Julian then Bob. Well that's where permutations comes in.
2:32 So combination gives you just a valid combination, it doesn't care about the order, right? Permutation will give you not only the valid combinations
2:44 but also in whatever possible order they can be in, okay and that's where permutations is super powerful, okay? So we'll do it the same thing,
2:56 we'll do the exact same things, let's just, may as well copy and paste. We'll do print(list(permutations()) works in the same way, see we got the
3:06 iterable and we got the r. We can go whoops, can't type. We can go friends and we'll do two as well, just so we're keeping this standard.
3:17 And look at that, we now have Mike and Bob, Mike and Julian, but then we also see, Bob and Mike, so over here we same Mike and Bob,
3:26 and over here now we see Bob and Mike. Then there's Bob and Julian, then there's the opposite, Julian and Mike, as opposed to Mike and Julian,
3:35 and Julian and Bob instead of Bob and Julian. And that's why permutations is awesome. I mean they're both awesome but this is such
3:44 a great way of doing it, it's one line of code, it's just amazing, if again, itertools is awesome. That is permutations and combinations

Talk Python's Mastodon Michael Kennedy's Mastodon