[Python-ideas] while conditional in list comprehension ?? (original) (raw)
Shane Green shane at umbrellacode.com
Mon Jan 28 15:32:21 CET 2013
- Previous message: [Python-ideas] while conditional in list comprehension ??
- Next message: [Python-ideas] while conditional in list comprehension ??
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Isn't "while" kind just the "if" of a looping construct?
Would [n for n in range(1,1000) while n < 400] == [n for n in range(1,1000) if n < 400]?
I guess your kind of looking for an "else break" feature to exit the list comprehension before evaluating all the input values. Wouldn't that complete the "while()" functionality?
Shane Green www.umbrellacode.com 408-692-4666 | shane at umbrellacode.com
On Jan 28, 2013, at 5:59 AM, Oscar Benjamin <oscar.j.benjamin at gmail.com> wrote:
On 28 January 2013 13:56, Chris Angelico <rosuav at gmail.com> wrote:
On Tue, Jan 29, 2013 at 12:33 AM, Wolfgang Maier <wolfgang.maier at biologie.uni-freiburg.de> wrote:
Why not extend this filtering by allowing a while statement in addition to if, as in:
[n for n in range(1,1000) while n < 400] The time machine strikes again! Check out itertools.takewhile - it can do pretty much that: import itertools [n for n in itertools.takewhile(lambda n: n<400, range(1,1000))] It's not quite list comp notation, but it works.
[n for n in itertools.takewhile(lambda n: n<40, range(1,100))] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39] The while clause is a lot clearer/nicer than takewhile/lambda. Presumably it would be more efficient as well. Oscar
Python-ideas mailing list Python-ideas at python.org http://mail.python.org/mailman/listinfo/python-ideas
-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130128/4c5352db/attachment.html>
- Previous message: [Python-ideas] while conditional in list comprehension ??
- Next message: [Python-ideas] while conditional in list comprehension ??
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]