Matt Logan
Matt Logan

Reputation: 5926

How can I make a list of n + 1 values with constant increment between two numbers?

I'm looking for a quick way to make a list of n + 1 values with constant increment between two numbers.

For example, if the input is:

min = 0
max = 10
n = 8

I want the output to be:

[0, 0.125, 0.250, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]

Currently, I'm using a function that looks like this:

def const_increment_list(min, max, n):
    increment = (max - min) / n
    return [min + i * increment for i in range(n + 1)]

Is this possible to do in a single line? My current method feels too verbose.

Edit:

You can use NumPy.

Upvotes: 1

Views: 1440

Answers (1)

Midnighter
Midnighter

Reputation: 3881

Have a look at numpy.linspace if you don't mind the dependency.

linspace(min, max, n+1)

Upvotes: 4

Related Questions