Hi. I'm reposting this here after erroneously posting it on python-dev. I use list comprehensions and generator expressions a lot and lately I've found myself writing a lot of code like this: for i in items if i.some_field == some_value: i.do_something() Naturally it won't work but it seems like a pretty straight-forward extension to allow compressing simple loops to fit on one line. The alternative, in my eyes, suggests there's something more happening than a simple include-test which makes it harder to comprehend. for i in items: if i.some_field == some_value: i.do_something() One possibility of course is to use a generator-expression but that makes it look like there are two for loops and it feels like a waste setting up a generator just for filtering. for i in (i for i in items if some_field == some_value): i.do_something() Stupid idea? Am I missing some obviously better way of achieving the same result? Thanks, Adde
2008/10/3 Andreas Nilsson
Hi. I'm reposting this here after erroneously posting it on python-dev.
I use list comprehensions and generator expressions a lot and lately I've found myself writing a lot of code like this:
for i in items if i.some_field == some_value: i.do_something()
I'm pretty sure this has been proposed before and that the consensus was that there was no advantage to writing: for i in L if cond: action() instead of: for i in L: if cond: action() -- Arnaud
participants (2)
-
Andreas Nilsson
-
Arnaud Delobelle