18

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]?

lospejos
  • 1,976
  • 3
  • 19
  • 35
Patrick
  • 697
  • 2
  • 9
  • 19

3 Answers3

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
12

range(10) is built in.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
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