- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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))

5

[1, 2, 3, 4, 5]

- Related Questions & Answers
- Python program to find N largest elements from a list
- Program to find duplicate element from n+1 numbers ranging from 1 to n in Python
- Python program to remove duplicate elements from a Doubly Linked List\n
- Get last N elements from given list in Python
- Program to find all missing numbers 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
- Python program to right rotate a list by n
- 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
- Program to print ‘N’ alphabet using the number pattern from 1 to n in C++
- Program to find kth lexicographic sequence from 1 to n of size k Python
- Java Program to Find sum of Series with n-th term as n^2 – (n-1)^2
- Program to delete n nodes after m nodes from a linked list in Python
- C Program to print numbers from 1 to N without using semicolon

Advertisements