Comprehension with two variables - explanation needed

Chris Angelico rosuav at
Sun Nov 23 16:42:16 CET 2014

On Mon, Nov 24, 2014 at 2:37 AM, Ivan Evstegneev
<webmailgroups at> wrote:
> As I know from books and googling, the comps main idea looks approximately
> like this:
> [target <--main loop<--nested loop/s (and maybe some conditions)]    Am I
> right?
> But your code looks somehow inverted to me o_0
> Like:
> [[target with nested  loop] <--- main loop with initial values for target]

Your analysis is correct, and it's because of the bracketing. Just as
you learned back in maths class (or math class, if you're that sort),
you start from the innermost brackets and work your way out. In fact,
the nested example starts with the main loop, and for each value it
goes through, it creates the entire inner comprehension.


More information about the Python-list mailing list