1

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

4
  • 2
    It's general reffered to as unpacking. tqdm is returning some kind of iterable which is being unpacked into the variables n and _id. Commented Apr 4, 2018 at 4:37
  • tqdm method returns a list which probably has a size of 2. When assigned to n, id_ 0th index is assigned to n and 1st index to id_ Commented Apr 4, 2018 at 4:40
  • 1
    @AnandSiddharth tqdm does not return a list. Commented Apr 4, 2018 at 4:52
  • 1
    There aren't two comma-separated values after the in here, there's just a single function call (with two arguments—and in fact, the second argument is a keyword argument, so it doesn't even really look like a second value). Commented Apr 4, 2018 at 4:52

2 Answers 2

2

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'
Sign up to request clarification or add additional context in comments.

6 Comments

just fyi, tqdm is a popular progress bar library.
@juanpa.arrivillaga What iterable of pairs does it yielding—maybe a percentage and a current label? (I'm not sure if that information would help clarify the answer or not; you probably know that better than me.)
@juanpa.arrivillaga Wait a second, looking at the way the OP is calling it, I'm guessing it just iterates the elements of what you pass it, and he's passing it an enumerate, so that's why he gets pairs?
I don't know much about how it is implemented, but it simply returns an iterable which will give you the values in the iterable you passed in, but will print a progress bar as a side-effect.
@ChristianDean I made sure to explain each part of what is and isn't going on, so whether the confusion is just bad wording or something more fundamental he should be able to find the answer he needs. And, more importantly, answers aren't just for the OP—if someone else has a problem that turns up this question in a search, they should get an answer that matches their question, not some different question the OP meant to ask but didn't so the search engine doesn't know what they intended.
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.