#100DaysOfCode in Python Transcripts
Chapter: Days 19-21: Iteration with itertools
Lecture: Itertools - Product

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's take a quick look at itertools product.
0:03 Now product actually is short for Cartesian product
0:08 or it's a Cartesian product, I suppose.
0:11 Now, what does that mean?
0:12 Well, it's not another language.
0:13 It actually means it is every
0:16 possible combination of values.
0:20 Alright, so think of it this way.
0:21 Let's say you had a string.
0:23 Let's say you had my name
0:25 and you wanted to see how many different possible
0:29 combinations you could get of the letters.
0:33 Now you can change that slightly.
0:36 So, the first thing that comes to mind is that you
0:39 might think how many combinations of six letters.
0:43 So my name, J-U-L-I-A-N.
0:46 How many combinations of those six letters can you get?
0:52 Now with itertools product,
0:54 that is exactly what it calculates for you.
0:57 That's what it prints out on the screen for you.
1:00 So, let me demonstrate that for you.
1:01 Okay, so we've done from itertools import product.
1:05 For letter, and we'll use my name
1:07 just because we've discussed that.
1:09 For letter in product, now in product,
1:12 look at that tool tip, we choose the iterable,
1:14 the item that we're iterating over and the repeat.
1:18 Now the repeat, it's actually quite easy.
1:21 It's much easier to show it to you.
1:22 But the repeat is essentially how many of those letters
1:27 or those items in that iterable
1:30 you want to show up as a product.
1:33 Okay, so let me show you.
1:34 So, we're going to use my name.
1:38 Julian.
1:39 And then, for the repeat, we're going to say 1,
1:42 just like the tall tip.
1:43 And when we hit enter, or when we, sorry I should say
1:47 when we print out the letter.
1:51 You'll see we, because we repeat it only 1,
1:54 we're only saying, well we only want one group,
1:58 1 grouping, we're only going to use how many iterations
2:01 of this, how many combinations of this are we getting to
2:07 a maximum of 1 character or 1 object?
2:12 Okay, so you can see here
2:14 Julian, the J can only show up once.
2:17 Okay, because it's only repeated once.
2:19 So, if we change this, let's just copy this back out.
2:26 And we change this to 2,
2:29 we can go print, letter.
2:33 Now, watch what happens.
2:35 We get a lot more as a result.
2:37 Okay, so the first thing it does is it takes the letter J.
2:41 Well, we're repeating 2.
2:42 We're going to use a combination of 2 letters.
2:44 We want to maximum of 2, it can repeat twice, okay.
2:48 So, we're going to have J with J.
2:51 We're going to have J with U.
2:52 We're going to have J with L.
2:53 We're going to have J with I.
2:54 And so on through the list.
2:55 And once it exhausts, J being in the first position
3:00 and this other combination being in the second position
3:05 it then moves down to the U.
3:07 And then it repeats it all over again.
3:09 And then, L and so on.
3:11 And it keeps going.
3:13 Now one thing you'll notice,
3:16 is that we're talking about the positioning here.
3:18 Okay, so U and I appear together here.
3:23 But they also appear together here.
3:26 I and U.
3:28 The difference being obviously that because they're in a
3:30 different order, it counts as a different combination.
3:35 Remember, this is every possible combination.
3:38 While yes, they're still returned as U and I
3:42 you're still getting a U and an I returned,
3:44 because they're in a different order,
3:45 because they're in a
3:46 different technical combination, I suppose.
3:49 They are capable of showing up twice.
3:52 Okay, what you will also notice,
3:56 is that J, J, does not show up twice.
4:02 Okay, it shows up once, because it doesn't matter.
4:06 These aren't treated as different J's.
4:09 It is a J, plain and simple.
4:11 Okay, so it shows up J, J, just once.
4:16 And that is editor's product.
4:19 It's so simple, but just imagine trying to code this
4:25 yourself with a for loop
4:26 and looking at if statements and everything like that.
4:29 It's disgusting, right.
4:31 So, this is the power of itertools with just two lines of
4:34 code you can get every possible combination,
4:39 the Cartesian product of an iterable.