I have a program with the following code:

    coros = [
        get_book(pair, session)
        for pair in PAIRS
    ]

How does this not give a syntax error? Or is it that if you do a for-loop in a list you have to put the function before the for-statement? I've never seen this but it's everywhere in the program.

upvote
  flag
> Or is it that if you do a for-loop in a list you have to put the function before the for-statement? yup, if the function is not defined, you'll get a NameError – Ayush Shanker

1 Answers 11

It's a list comprehension written in a non-standard way. The way it would usually be written is:

[get_book(pair, session) for pair in PAIRS]

It's equivalent to:

array = []
for pair in PAIRS:
    array.append(get_book(pair, session))

Not the answer you're looking for? Browse other questions tagged or ask your own question.