1-liner to iterate over infinite sequence of integers?

Robert Kern robert.kern at gmail.com
Thu Oct 13 13:55:25 EDT 2005


Neal Becker wrote:
> I can do this with a generator:
> 
>     def integers():
>         x = 1
>         while (True):
>             yield x
>             x += 1
> 
> for i in integers(): 
> 
> Is there a more elegant/concise way?

from itertools import count

for i in count():
    ...

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the Python-list mailing list