Why don't you have to use zip()

I had a question on the “Sampling Mission” on Page 7. URL: Learn data science with Python and R projects

Below is the for-loop:

for stratum, position in [(stratum_G, 'G'), (stratum_F, 'F'), (stratum_C, 'C'),
                (stratum_GF, 'G/F'), (stratum_FC, 'F/C')]:

I thought when we are looping two or more things we needed to use the zip() function. I thought it would look like this:

for stratum, position in zip((stratum_G, 'G'), (stratum_F, 'F'), (stratum_C, 'C'),
                (stratum_GF, 'G/F'), (stratum_FC, 'F/C')):

Why don’t we have to use zip()?

In short, because that’s not how zip() works! :sunglasses:

To compare, we could use zip() here by doing the following:

strata = [stratum_C, stratum_F, stratum_G, stratum_GF, stratum_FC]
pos = ['C', 'F', 'G', 'GF', 'FC']
for stratum, position in zip(strata, pos):

“Simply” put, when we declare more than one variable in a for loop to loop over a list (more specifically, to loop over an iterable), python assumes that each element in the list (iterable) contains the same number of elements as the number of variables we declared in the for statement. Therefore, we only need to use zip() if the iterable we are looping over doesn’t have the same number of elements per element as we have variables.

In this case, we are looping over a list that has two elements per element in the list, so we’re good to go without zip()!

1 Like

Hi @mathmike314,
Thanks for the reply. I didn’t see this until now #BetterLateThanNever