How To Create A List Of Numbers From 1 to N in Python

It is not hard to create a list of numbers from 1 to N in Python. In this article, we share with you the top 3 ways to do this work. Read this article to learn and apply our tips immediately to your Python projects!

3 ways to create a list of numbers from 1 to N in Python

Below are the top 3 ways to create a list of numbers from 1 to N in Python.

Use for loop and .append() method

The most basic way is to combine the for loop and the .append() method. The for loop allows you to adjust the value of a number variable from 1 to N. The .append() method helps you push a number to the end of a list.

The syntax is like this:

[list number] = []

for [number] in range(1, N+1):
     [list number].append([number])

The first thing you need to do is create an empty list [list number]. After that, the variable [number] will move from 1 to N in the for loop. For each time the [number] variable gets a new value, it will be moved to the end of the [list number].

Here is the code sample:

# Create an empty list number
lstNumber = []

# Use loop and .append() to push numbers from 1 - 9 to the list
for number in range(1, 10):
    lstNumber.append(number)
print(lstNumber)

The output will be:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Use while loop and .append() method

If you don’t like to use the for loop, the while loop is another option. The algorithm is the same as you use the while loop to adjust the value of the number variable. And for each time the value changes, you add it to the list.

The syntax is like this:

[lists number] = []
[number] = 1

while [number] <= N:
   [list number].append([number])
   [number] += 1

You also need to create an empty list [list number]. It is important to create a variable [number] and assign the value 1 to it. In the while loop, you append the value of the [number] to the end of the [list number] and then add 1 to it.

See the code sample below:

# Create an empty list number
lstNumber = []

# Create a number variable starting from 1
number = 1

# Use loop and .append() to push numbers from 1 - 9 to the list
while number <= 9:
    lstNumber.append(number)
    number += 1
print(lstNumber)

The output will be:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Use the numpy.arange() function of the numpy module

Python has a numpy module that features the numpy.arange() function. The numpy.arange() helps you create a list of numbers from 1 to N quickly.

The first thing to do is import the numpy module to your Python program:

import numpy as np

And then, follow the syntax below to take advantage of the numpy.arange() function:

[list number] = list(np.arange(1, N+1))

The list() allows you to create a list. And the np.arange(1, N+1) creates the numbers from 1 to N inside this list. You store all the values in a variable [list number].

Here is the code sample:

import numpy as np

# Create a list and use the numpy.arange(1, 10) to add numbers from 1 - 9
lstNumber = list(np.arange(1, 10))
print(lstNumber)

The output will be:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Summary

To summarize, we have taught you 3 ways to create a list of numbers from 1 to N in Python. You can use 2 basic ways, which are using for and while loops to do this work. Otherwise, you can import the numpy module and take advantage of the numpy.arange() feature.

Leave a Reply

Your email address will not be published. Required fields are marked *