The easiest way to implement this is a generator function:

Use with AIfrom __future__ import division

def linspace(start, stop, n):
    if n == 1:
        yield stop
        return
    h = (stop - start) / (n - 1)
    for i in range(n):
        yield start + h * i

Example usage:

Use with AI>>> list(linspace(1, 3, 5))
[1.0, 1.5, 2.0, 2.5, 3.0]

That said, you should definitely consider using NumPy, which provides the numpy.linspace() function and lots of other features to conveniently work with numerical arrays.

Answer from Sven Marnach on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › alternatives to numpy logspace and linspace?
Alternatives to Numpy logspace and linspace? : r/learnpython
March 14, 2016 - Not necessarily. If you are going to do python for loops, using numpy arrays is usually slower unless numpy is doing some really heavy math to calculate the array.
🌐
GitHub
gist.github.com › pmav99 › d124872c879f3e9fa51e
Pure python linspace / logspace implementation · GitHub
Pure python linspace / logspace implementation · Raw · linspace.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Find elsewhere
🌐
Real Python
realpython.com › np-linspace-numpy
np.linspace(): Create Evenly or Non-Evenly Spaced Arrays – Real Python
July 3, 2026 - However, even using a list comprehension is rather clumsy and inelegant compared to using np.linspace(). You first need to work out the interval required and then use that interval within a loop. In most applications, you’ll still need to convert the list into a NumPy array since element-wise computations are less complicated to perform using NumPy arrays. Another point you may need to take into account when deciding whether to use NumPy tools or core Python is execution speed.
🌐
Stack Overflow
stackoverflow.com › questions › 73659121 › linspace-in-python
matlab - Linspace in Python? - Stack Overflow
Typing "python linspace" in google gives you the answer....... ... Save this answer. ... Show activity on this post. As Bylal said it's a numpy thing. Numpy mostly exists for greater easy of use and performance in mathematical functions.
🌐
CASRAI
casrai.org › guides › research tools & software › statistical software for research › linspace matlab: syntax, vectors and alternatives
Linspace MATLAB: Syntax, Vectors and Alternatives — CASRAI
5 days ago - In open-source environments, GNU Octave provides an identical linspace implementation, while Python offers numpy.linspace. The function of linspace is to provide deterministic, uniform discretization of a numerical interval without requiring manual step calculations or loop iterations.
🌐
Finxter
blog.finxter.com › home › learn python blog › how to create a sequence of evenly spaced values [vanilla python & numpy linspace]
How to Create a Sequence of Evenly Spaced Values [Vanilla Python & Numpy Linspace] - Be on the Right Side of Change
January 21, 2021 - print([int(x) for x in np.linspace(start, stop, num_vals)]) # [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] ... Numpy is a popular Python library for data science focusing on linear algebra.
🌐
DataCamp
datacamp.com › home › tutorials › python › how to use the numpy linspace() function
How to Use the NumPy linspace() Function | DataCamp
April 5, 2024 - If you’re in a hurry, here’s the quickest explanation of the linspace() function. NumPy's linspace() function generates an array of evenly spaced numbers over a defined interval. Here, for example, we create an array that starts at 0 and ends at 100, throughout an interval of 5 numbers.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy-linspace
NumPy linspace() Method | Create Evenly Spaced Array - GeeksforGeeks
January 24, 2025 - Unlike the range() function in Python that generates numbers with a specific step size. linspace() allows you to specify the total number of points you want in the array, and NumPy will calculate the spaci
🌐
Reddit
reddit.com › r/julia › how do you do the equivalent of linspace?
r/Julia on Reddit: How do you do the equivalent of linspace?
January 31, 2021 -

I have to do a lot of plotting and I always make it such that if I have to vary some parameter everything else adjusts accordingly, but sometimes when I change parameters there's issue with the number of elements in lists which prevents me from plotting along with a whole bunch of other things. (right now I'm using a:0.01:b)

So how do I get a list of n evenly spaced elements from a to b like in

np.linspace(a,b,n)

???

🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-linspace
NumPy linspace() Method | Create Evenly Spaced Array - GeeksforGeeks
February 14, 2026 - By default, numpy.linspace() includes the stop value as the last element of the array. This behavior can be changed using the endpoint parameter.
🌐
Expert Python
expertpython.fr › lexique › linspace()
linspace() - Expert Python
La fonction linspace() est couramment utilisée pour générer des tableaux de nombres uniformément espacés sur un intervalle spécifié.
🌐
YouTube
youtube.com › watch
Graphing in Python with the linspace() Function - YouTube
As part of a series on Python list operations, plotting and drawing, this NCLab video demonstrates how to set intervals on a graph, build x arrays, and draw ...
Published: September 13, 2018
🌐
GeeksforGeeks
geeksforgeeks.org › numpy-linspace-python
numpy.linspace() in Python - GeeksforGeeks
May 19, 2022 - The numpy.linspace() function returns number spaces evenly w.r.t interval. Similar to numpy.arange() function but instead of step it uses sample number.