You cannot exactly do what you want in understanding the list, but since you want to generate some elements while they meet the condition, it is better to create a generator from what you want (which creates elements on demand) and then filter your generator with itertools.takewhile
.
>>> from itertools import takewhile
>>> list(takewhile(lambda x:x<5,range(5)))
[0, 1, 2, 3, 4]
source
share