. Advertisement .
. Advertisement .
Python is a general-purpose, high-level programming language created by Guido van Rossum and first released in 1991. Python is designed with the strong advantage of being easy to read, learn, and remember.
“How to make list of Numbers From 1 to N in Python” is one of the most common programming questions. So, what can we do? We will discuss this question to find the best solution.
How to make list of Numbers From 1 to N in Python
Solution 1: Use the range()
You can make list of Numbers From 1 to N in Python by using range(). So, without further ado, let us learn about this by using the following example:
mylst = list(range(5,10+1)) print(mylst)
[5, 6, 7, 8, 9, 10]
Solution 2: Use for loop
In this method, you also can create a list of numbers from 1 to N by using a for loop. Let’s follow this example to understand more this solution.
def createList(n): lst =  for i in range(n+1): lst.append(i) return(lst) print(createList(6))
[0, 1, 2, 3, 4, 5, 6]
Solution 3 : Use the numpy.arange()
Many useful array creation and modification methods are available in the NumPy module. This module’s
arange() function is similar to the
range() function discussed previously. The result is a numpy array.
This function will be implemented in the code below.
import numpy as np lst = list(np.arange(1,10+1)) print(lst)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list() function is also used to convert the final output to a list form.
Solution 4: Follow this process
Besides these above solutions, you also can make list of Numbers From 1 to N in Python by following this process:
First, you can utilize the list comprehension strategy of Python to handle this issue.
The next step, you can use the
range() method to create a list with
i for each
n. Because you wish to create up to
n, the lower bound will be
n in this case, and the upper bound will be
To further understand, let’s look at the below implementation.
def solve(n): return [i for i in range(1,n+1)] n = 5 print(solve(n))
[1, 2, 3, 4, 5]
Python is widely used in web applications, software development, data science, and machine learning (ML). Developers use Python because it’s efficient, easy to learn, and run on various platforms. Python software is free to download, integrates well with all systems, and speeds up development. Keep reading if you’re still stumped by the question “How to make list of Numbers From 1 to N in Python“. The above options are the most practical.
If you still require assistance or have problems, we have a large community where everyone is generally eager. Last but not least, we wish all users an effective day full of new code solutions.
Leave a comment