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

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