Generating an ascending list of numbers of arbitrary length in python
69,118
Solution 1
You want range()
.
Solution 2
range(10)
is built in.
Solution 3
If you want an iterator that gives you a series of indeterminate length, there is itertools.count()
. Here I am iterating with range()
so there is a limit to the loop.
>>> import itertools
>>> for x, y in zip(range(10), itertools.count()):
... print x, y
...
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
Later: also, range() returns an iterator, not a list, in python 3.x. in that case, you want list(range(10))
.
Related videos on Youtube
Author by
Patrick
Updated on July 09, 2022Comments
-
Patrick almost 2 years
Is there a function I can call that returns a list of ascending numbers? I.e.,
function(10)
would return[0,1,2,3,4,5,6,7,8,9]
? -
jupiar over 5 yearsAnswer is so tiny, and making it into a function would look like:
def my_func(min, max): return list(range(min, max))
-
PeJota about 2 years@jupiar your comment should be the top level answer