0

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.

user3605780
  • 6,542
  • 13
  • 42
  • 67
  • > `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 Jan 14 '18 at 12:02

1 Answers1

0

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))
Fredrick Brennan
  • 7,079
  • 2
  • 30
  • 61