26

Does python have a native iterable of the infinite integer series?

I've tried range(float('inf')) and iter(int), but neither work.

I can obviously implement my own generator along the lines of

def int_series(next=1):
    while True:
        next += 1
        yield next

but this feels like something which should already exist.

Bach
  • 6,145
  • 7
  • 36
  • 61
jackweirdy
  • 5,632
  • 4
  • 24
  • 33
  • In what practical purposes would you need an `inf` – sshashank124 Apr 07 '14 at 14:00
  • 18
    How is this "primarily opinion-based"? – arshajii Apr 07 '14 at 14:01
  • 1
    I was about to say the same thing. It's absurd to vote to close such a coherent question. – keyser Apr 07 '14 at 14:02
  • 2
    @sshashank124 In Haskell we use infinite lists all the time. Of course it's not strictly *necessary*, but it allows for some elegant solutions – Niklas B. Apr 07 '14 at 17:38
  • @sshashank124 Or are you asking when the float value `infinity` is useful? Everytime you need an `R` such that `abs(x) < R` for all finite `x`. For example it is the neutral element of the minimum monoid – Niklas B. Apr 07 '14 at 17:41

2 Answers2

41

Yes. It's itertools.count:

>>> import itertools
>>> x = itertools.count()
>>> next(x)
0
>>> next(x)
1
>>> next(x)
2
>>> # And so on...

You can specify start and step arguments, though stop isn't an option (that's what xrange is for):

>>> x = itertools.count(3, 5)
>>> next(x)
3
>>> next(x)
8
>>> next(x)
13
user2357112
  • 260,549
  • 28
  • 431
  • 505
16

You can use itertools.count for this.

for x in itertools.count():
    # do something with x infinite times

If you don't want to use the integer returned by count(), then better use itertools.repeat:

for _ in itertools.repeat(None):
     # do something infinite times
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 7
    If you don't want the integer, `while True` is much more appropriate. – user2357112 Apr 07 '14 at 14:06
  • @user2357112 While loop can be twice as slow compared to a for-loop. – Ashwini Chaudhary Apr 07 '14 at 14:13
  • 6
    If that tiny overhead really matters, `while 1` is compiled to an unconditional jump, which is even faster than the for loop. Most likely, the time taken by the loop body will dominate and the difference won't matter. – user2357112 Apr 07 '14 at 14:18
  • 4
    Overhead is irrelevant for a loop that’s going to run an infinite number of times ;-) – bdesham Apr 07 '14 at 18:06
  • 3
    @user2357112 In python3.3 `while True` compiles to **exactly** the same instructions as `while 1` (i.e. `SETUP_LOOP` + `JUMP_ABSOLUTE`). In python2.7 there is a difference. I don't have previous versions of python3 to check, but I believe already in python3.0 they were equivalent. – Bakuriu Apr 07 '14 at 18:21
  • @Aशwiniचhaudhary Because `for` doesn't check any conditions, it just waits for the iterator supplied to it to throw a `StopIteration` exception, right? I could see that posing a problem if the `while` condition were complicated, but terminating generators/iterators have their own termination logic internally anyway so I'm not sure it makes much of a difference for the vast majority of cases unless the internal logic is also exception-driven. – JAB Apr 07 '14 at 19:27