Making a list of evenly spaced numbers in a certain range in python

ListRangePython

List Problem Overview


What is a pythonic way of making list of arbitrary length containing evenly spaced numbers (not just whole integers) between given bounds? For instance:

my_func(0,5,10) # ( lower_bound , upper_bound , length )
# [ 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5 ] 

Note the Range() function only deals with integers. And this:

def my_func(low,up,leng):
    list = []
    step = (up - low) / float(leng)
    for i in range(leng):
        list.append(low)
        low = low + step
    return list

seems too complicated. Any ideas?

List Solutions


Solution 1 - List

Given numpy, you could use linspace:

Including the right endpoint (5):

In [46]: import numpy as np
In [47]: np.linspace(0,5,10)
Out[47]: 
array([ 0.        ,  0.55555556,  1.11111111,  1.66666667,  2.22222222,
        2.77777778,  3.33333333,  3.88888889,  4.44444444,  5.        ])

Excluding the right endpoint:

In [48]: np.linspace(0,5,10,endpoint=False)
Out[48]: array([ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5])

Solution 2 - List

You can use the following approach:

[lower + x*(upper-lower)/length for x in range(length)]

lower and/or upper must be assigned as floats for this approach to work.

Solution 3 - List

Similar to unutbu's answer, you can use numpy's arange function, which is analog to Python's intrinsic function range. Notice that the end point is not included, as in range:

>>> import numpy as np
>>> a = np.arange(0,5, 0.5)
>>> a
array([ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5])
>>> a = np.arange(0,5, 0.5) # returns a numpy array
>>> a
array([ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5])
>>> a.tolist() # if you prefer it as a list
[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]

Solution 4 - List

f = 0.5
a = 0
b = 9
d = [x * f for x in range(a, b)]

would be a way to do it.

Solution 5 - List

You can use the folowing code:

def float_range(initVal, itemCount, step):
    for x in xrange(itemCount):
        yield initVal
        initVal += step

[x for x in float_range(1, 3, 0.1)]

Solution 6 - List

Numpy's r_ convenience function can also create evenly spaced lists with syntax np.r_[start:stop:steps]. If steps is a real number (ending on j), then the end point is included, equivalent to np.linspace(start, stop, step, endpoint=1), otherwise not.

>>> np.r_[-1:1:6j, [0]*3, 5, 6]
array([-1. , -0.6, -0.2,  0.2,  0.6,  1.])

You can also directly concatente other arrays and also scalars:

>>> np.r_[-1:1:6j, [0]*3, 5, 6]
array([-1. , -0.6, -0.2,  0.2,  0.6,  1. ,  0. ,  0. ,  0. ,  5. ,  6. ])

Solution 7 - List

Similar to Howard's answer but a bit more efficient:

def my_func(low, up, leng):
    step = ((up-low) * 1.0 / leng)
    return [low+i*step for i in xrange(leng)]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDouble AAView Question on Stackoverflow
Solution 1 - ListunutbuView Answer on Stackoverflow
Solution 2 - ListHowardView Answer on Stackoverflow
Solution 3 - ListmilancurcicView Answer on Stackoverflow
Solution 4 - ListmarcView Answer on Stackoverflow
Solution 5 - ListArtsiom RudzenkaView Answer on Stackoverflow
Solution 6 - ListRobin DinseView Answer on Stackoverflow
Solution 7 - ListPeter CollingridgeView Answer on Stackoverflow