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]
?
Asked
Active
Viewed 8.2k times
3 Answers
34
You want range()
.
def my_func(min, max)->list:
return list(range(min, max))

Johan
- 74,508
- 24
- 191
- 319

Ignacio Vazquez-Abrams
- 776,304
- 153
- 1,341
- 1,358
-
11Answer is so tiny, and making it into a function would look like: `def my_func(min, max): return list(range(min, max))` – alexexchanges Oct 23 '18 at 04:30
-
@jupiar your comment should be the top level answer – PJ_ Apr 04 '22 at 19:10
-
This answer needs to be updated. Because in Python 3 (I think): – Lu Kas Jul 06 '22 at 12:33
9
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))
.

hughdbrown
- 47,733
- 20
- 85
- 108