3 ms·
Well, Google doesn't recommend it either (http://google-styleguide.googlecode.com/svn/trunk/pyguide.html?showone=List_Comprehensions#List_Comprehensions http://
by jevgeni 12y ago
Well, Google doesn't recommend it either (http://google-styleguide.googlecode.com/svn/trunk/pyguide.html?showone=List_Comprehensions#List_Comprehensions http://google-styleguide.googlecode.com/svn/trunk/pyguide.ht...), but I guess they are all bloody noobs or whatever.
I mean, single level comprehension is good. Nested list comprehension is OK only in most trivial cases. In my opinion, if I see how a person uses list comprehension, I can tell, what kind of person this is.
There are people who, for example, do this
def all_is_okey_dorey(lst):
return all([some_predicate_fn(x) for x in lst])
instead of this
def all_is_okey_dorey(lst):
for x in lst:
if not some_predicate_fn(x):
return False
return True
and can live with themselves somehow.
Or there are people, who refuse to acknowledge the existence of anything besides Python 3.x and when forced to write in 2.x use list comprehension instead of iterator comprehension.
Thing is, the validity of using nested list comprehension depends not on the amount of for loops you have, but on the thing you want to do with the item. If it's just selection, then it might be ok. If you want to apply some kind of function to it, then it's most probably the case of trying to be too clever.
- jules 12y agoThe only thing wrong with that list comprehension version is those [ ] all(some_predicate_fn(x) for x in lst) Much better than the loop.
- hueving 12y agoIf you think two loops is not a "simple case" of a list comprehension as Google suggests to use them for, perhaps you shouldn't be doing code reviews. It sounds like your team would be held down by your weak grasp of the language.