News for hackers. Stuff that matters.
Re:Python List Comprehensions Explained Visually by sanderjd on Wednesday December 02, 2015 @06:57PM (#10666792)
I'm a big fan of just using map/filter, but I think it makes sense to wonder whether such a common and important operation is worth its own syntax. I do think the way nesting works is a bit surprising. It didn't really occur to me until reading an earlier comment that in essence the most nested loop is a `map` and all the others are `flatMap`s. (I think?)
Re:Python List Comprehensions Explained Visually by d0mine on Wednesday December 02, 2015 @07:06PM (#10666833)
The way to understand how the nesting works in the list comprehension:

   L = []
   for x in X:
       for y in Y:
          L.append(x*y)
[loops] translate to

  L = [x*y for x in X for y in Y]
If you understand the order of the loops in the first code fragment; you should understand the order in the second one -- it is the same order.
Re:Python List Comprehensions Explained Visually by dragonwriter on Wednesday December 02, 2015 @08:34PM (#10667155)
The problem is that the relation of the body to the loop specifications is reversed, so the inner loop is most proximate to the body in the first, but most distant from the body in the second.

The specification of the inner loop should always be most proximate to the body, not last independent of the location of the body.

Re:Python List Comprehensions Explained Visually by sanderjd on Thursday December 03, 2015 @06:39PM (#10673575)
As a person who thinks about this stuff in more functional terms, as in mapping over collections, rather than imperatively iterating and appending, it helps me to realize how it translates to map and flatMap rather than how it translates to for loops (which I already realized).