list with infinite elments

This is where the iterator comes in. You can't have an infinite list of numbers, but you can have an infinite iterator.

import itertools
arithmetic_progression = itertools.count(start,step) #from the python docs

The docs for Python2 can be found here


You are looking for a python generator instead:

def infinitenumbers():
    count = 0
    while True:
        yield count
        count += 1

The itertools package comes with a pre-built count generator.

>>> import itertools
>>> c = itertools.count()
>>> next(c)
0
>>> next(c)
1
>>> for i in itertools.islice(c, 5):
...     print i
...
2
3
4
5
6

Tags:

Python