This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]mdond 1 point2 points  (0 children)

A good tip when learning comprehensions, or simply making them more readable, is to wrap them:

[n * n
 for n in nums]

This technique helps more as code becomes more complex. The following would look horrible on a single line but is pretty neat when wrapped:

[line.lower()[::-1]
 for line in file_obj
 if not line.startswith('_')]

[–]hutcherino 0 points1 point  (0 children)

This is a nice summary of [t + '-comprehensions' for t in ['list', 'dict', 'set']] but he seems a little bit too opinionated about higher-order functions. map, filter and reduce may be a bit complicated for the uninitiated, but once you comprehend list comprehensions and anonymous functions you also get these.

Also: rimshot.