Itertools.takewhile but in a list comprehension

Call me a heretic if you will, but the cleanest way to maintain the duality would be to change for loops rather than comprehensions.

[item for item in iterable while item < 10]
# <==>
for item in iterable while item < 10:
	yield item

It’s a bit of an unusual use-case though, so I don’t expect anyone to like the idea. Still, it does let you maintain the duality…

1 Like