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

Talk Python's Mastodon Michael Kennedy's Mastodon