-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Are list comprehensions pythonic? #167
Comments
PEP 202 says:
Interestingly GVR says using map with lambda as one of his "regrets" and to use a list comprehension instead. Then in the very next section he says a for loop is more clear than reduce().
Also Guido said:
Timing map with a function vs map with a lambda: |
I agree with @robert-wallis. The most simple list comprehension does read more difficult that a slightly complexer one: b = [ i*i for i in my_list if i > 4 ] |
Going off of what @robert-wallis said, I think we should move the |
I use map every day :) |
I think list comprehension (as other functional sides of Python) are not the most "n00b" friendly feature, but once you get them you will migrate any old code to use them, so I would stick to teach them as a good thing, a little less easy to grasp but definitely good. |
The relevant part starts at 2:55. You should watch the whole video regardless. |
While I'd agree list comprehensions are generally nice, and make one feel very clever when using them, are they necessarily more pythonic than their counterparts?
In your section "Short Ways to Manipulate Lists" you call the following code "Bad",
and the following list comprehensions "Good",
The section goes on to include more examples.
I would argue that calling the simpler code "Bad" simply because it takes up more space and doesn't include lambdas/one liners gives the wrong message, especially to beginners. Following the Zen of Python, specifically "Explicit is better than implicit", "Simple is better than complex", and "Readability counts" the list comprehensions could be qualified as less pythonic than the more explicit nested conditional clauses. However, this could be refuted by "Flat is better than nested".
I could be wrong, just thought I would bring it up.
The text was updated successfully, but these errors were encountered: