- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

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

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]

- Related Articles
- Python program to find N largest elements from a list
- Python program to remove duplicate elements from a Doubly Linked List\n
- Program to find duplicate element from n+1 numbers ranging from 1 to n in Python
- Python Program to Reverse only First N Elements of a Linked List
- C# program to create a List with elements from an array
- Get last N elements from given list in Python
- Program to find all missing numbers from 1 to N in Python
- Python program to right rotate a list by n
- Program to delete n nodes after m nodes from a linked list in Python
- Python Program to Create a Linked List & Display the Elements in the List
- Python Program to extracts elements from a list with digits in increasing order
- Python program to remove Duplicates elements from a List?
- Python Program to Remove Palindromic Elements from a List
- Python program to count total set bits in all number from 1 to n.
- Find four missing numbers in an array containing elements from 1 to N in Python

Advertisements