# Program to create a list with n elements from 1 to n in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a number n. We have to create a list of elements of size n, the elements are from 1 to n.

So, if the input is like n = 5, then the output will be [1,2,3,4,5]

To solve this, we will follow these steps −

• use python list comprehension strategy to solve this problem
• create a list with i for each i from 1 to n, for this we use range() function. This will take lower bound which is n here, and upper bound n+1 because we want to generate up to n.

## Example

Let us see the following implementation to get better understanding −

def solve(n):
return [i for i in range(1,n+1)]
n = 5
print(solve(n))

## Input

5


## Output

[1, 2, 3, 4, 5]