# How to Generate List of Numbers from 1 to N

To generate a list of numbers from 1 to N in Python, use the range() function. The range() is a built-in Python method that returns a sequence of numbers, starting from 0 by default, increments by 1 (by default), and stops before a provided number.

The range() function generates value from start number to end number − 1. If we convert those values to a list using list(), we get a list containing N values starting from 1.

## Python program to generate numbers from 1 to N

``````n = int(input("Enter N: "))
l = list(range(1, n+1))
print(l)``````

#### Output

``````Enter N: 10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]``````

You can see that the range() function generates values starting from 1 to N-1. So, I add 1 in the ending value and convert it into a list using list(). The list() method ensures that the final result is in a list form.

### Create a list of numbers from 1 to N using List Comprehension

To create a list in Python, use the list comprehension approach. A list comprehension is a short-hand approach where you can generate a list of numbers from 1 to N. It is a concise and straightforward way to make a list.

#### Syntax

``list = [var for var in expression]``

#### Code

``````num = int(input("Enter N: "))
lc = [x for x in range(1, num+1)]
print(lc)``````

#### Output

``````Enter N: 11
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]``````

In this code, the range() function generates values from 1 to N+1, and list comprehension creates a list out of that numbers.

## Using user-defined function

To create a user-defined function in Python, use the def keyword. For example, we will create a function that accepts the “N” from the user and make a list that contains 1 to N numbers and return that list.

### Code

``````def makeList(n) -> list:
list_data = []
for i in range(1, n+1):
list_data.append(i)
return(list_data)

n = int(input("Enter N: "))
lst = makeList(n)
print(lst)``````

#### Output

``````Enter N: 10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]``````

We used a list() function inside the makeList() method to create an empty list in this code.

After that, we used a for loop and range() function, which generates a value from 1 to N, and inside the for loop, we appended value from 1 to N, one by one in the list, and in the end, we will get the list of numbers starting 1 to N.

## Creating a list using the numpy arange() function

The numpy arange() is a library function that creates an array. Python does not have a built-in array data type, but we can create an array using a numpy library.

The numpy provides a function called arange() similar to Python’s range() function. I wrote a brief tutorial on the difference between range() vs np.arange().

The output of numpy operations will be an array, and we can convert an array to a list using a list() function.

``````import numpy as np

n = int(input("Enter N: "))
l = list(np.arange(1, n+1))
print(l)``````

#### Output

``````Enter N: 11
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]``````

To use a Python module in your program, use the “import” keyword to import the module. For example, we imported the numpy module, created an array of 1 to N numbers using the arange() function, and converted that array into the list using the list() function.

## Conclusion

Generating a list of numbers from 1 to N is the most common task in programming, and we saw four ways to achieve the goal. Python has a built-in function and a third-party library function that will help you create and modify a list and array. That’s it for this tutorial.