Questions: While And List Comprehension

Peter Otten __peter__ at web.de
Thu Nov 11 09:10:21 CET 2010


James Mills wrote:

> On Thu, Nov 11, 2010 at 11:01 AM, alex23 <wuwei23 at gmail.com> wrote:
>> +1 on this approach. Clear and obvious and not reliant on any library
>> modules other than sys.
>>
>> itertools, what WAS I thinking? :)
> 
> maybe:
> 
> import sys
> from itertools import islice
> 
> print [v for v in islice((line for line in sys.stdin), 0, 5)]

(line for line in sys.stdin)

and sys.stdin are equivalent as are

[v for v in items] 

and list(items)

So 

print list(islice(sys.stdin, 5))

Peter



More information about the Python-list mailing list