News for hackers. Stuff that matters.
Re:Python List Comprehensions Explained Visually by d0mine on Wednesday December 02, 2015 @07:00PM (#10666803)
It is not much easier to understand the explicit loop:

  doubled_odds = []
  for n in numbers:
       if n % 2 == 1:
           doubled_odds.append(n * 2)
compared to the list comprehension:

  doubled_odds = [n * 2 for n in numbers if n % 2 == 1]
Here's the explicit loop without newlines for comparison:

  for n in numbers: if n % 2 == 1: doubled_odds.append(n * 2)
Notice that the for-loop and the if-statement are in the same order in the loop and in the list comprehension. Only the value expression (n * 2) is out-of-order. OPs jumping around is often unnecessary in the gif.

The one line list comprehension is as much readable for people familiar with Python as the variant with the explicit loop.

It is not uncommon to use several list comprehensions in a function. If you rewrite them as explicit loops then it may increase the line count 4 times e.g., it can convert a simple 3-lines function that could be understood at a glance into 12 lines function that you have to read line by line to understand -- it is easy to keep track of 3 values on 3 lines but it is not clear whether it is true for 12 lines.

Re:Python List Comprehensions Explained Visually by iamsohungry on Wednesday December 02, 2015 @10:30PM (#10667561)
I think the problem here is that nested comprehensions and filter comprehensions are almost always too complex. If you are doing something that requires that, you should break it up and give the pieces names:

    odd_numbers = range(1, limit, 2)
    doubled_odd_numbers = [n * 2 for n in odd_numbers]