William Johnson
William Johnson

Reputation: 15

python for loop multiple values / objects? after 'in'

I cannot figure out what the syntax is doing is this for loop:

for n, id_ in tqdm(enumerate(test_ids), total=len(test_ids)):

I found it in a Kaggle Kernel. I tried searching as much as possible for a solution, but could not find any examples or explanations for python for loops where there are multiple comma separate values after the "in" in part of a for loop. And since I don't know what this is called, I'm not sure what else to search for.

This is the whole block of code:

for n, id_ in tqdm(enumerate(test_ids), total=len(test_ids)):
    path = TEST_PATH + id_
    img = imread(path + '/images/' + id_ + '.png')[:,:,:IMG_CHANNELS]
    sizes_test.append([img.shape[0], img.shape[1]])
    img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
    X_test[n] = img

This is the kernel where the code is from: https://www.kaggle.com/keegil/keras-u-net-starter-lb-0-277?scriptVersionId=2164855

Upvotes: 1

Views: 1297

Answers (2)

MSPO
MSPO

Reputation: 120

What you have happening in this for loop is called unpacking and zipping. You can ignore the tqdm function because if you were to remove it the only thing that would be different is you wouldn't see a load bar but everything else would work just fine. This code

for n, id_ in tqdm(enumerate(test_ids), total=len(test_ids)):

works the same as just without a load bar.

for n, id_ in enumerate(test_ids):

what enumerate does is places the index number of each item in a tuple that is given in a new dimension of the array. So if the array input was 1D then the output array would be 2D with one dimension the indexes and the other the values. lets say your test_ids are [(2321),(2324),(23213)] now with enumerate the list now looks like [(0,2321),(1,2324),(2,23213)] what putting a comma does is once given a value (from our case the for loop) say (0,2321) is separate each tuple value in the order they are given so in this case they would equal n = 0 and id_ = 2321

Upvotes: 1

abarnert
abarnert

Reputation: 365697

You can use multiple comma-separated elements for the iterable in a for loop—but that isn't what's happening here. There's just one thing after the in, a function call that happens to take two arguments.

Compare with this case:

for x in range(0, 10):

There aren't two comma-separated values 0 and 10 on the right side here, just one range.


What if you do have two or more comma-separated values on the right? Then it's just a tuple:

>>> for x in 1, 2, 3:
...     print(x)
1
2
3

It's the exact same tuple as in for x in (1, 2, 3):.


Making things slightly more complicated in your case is that the individual elements of whatever tqdm returns are being unpacked. (In this case, it yields whatever's passed in, and since you passed in an enumerate, each element is an index-value pair.) So there are two comma-separated targets on the left side of the in.

But that's a completely separate issue, which you can reproduce with just a simple list to loop over:

>>> word = ['aa', 'ab', 'bc']
>>> for first_letter, second_letter in words:
...    print(first_letter, second_letter)
a a
a b
b c

The unpacking is the same as in an assignment:

>>> first, second = 'az'
>>> first
'a'
>>> second
'z'

Upvotes: 2

Related Questions