In this article, we will discuss how to create a Python List of numbers from 1 to N.

**Table Of Contents**

## Introduction

Suppose value of N is 15, then the list should contain numbers from 1 to 15. Like this,

[1, 2, 3, 4, 5, 6 ,7, 8, 19, 10, 11, 12, 13, 14, 15]

There are different ways to create a list containing numbers from 1 to N. Let’s dicuss them one by one,

## Method 1: Using range() function

The range() function in Python, accepts three arguments i.e. start, stop and step. It returns a sequence of integers from **start** (inclusive) to **stop** (exclusive) by given **step**.

**Advertisements**

To create a list of numbers from 1 to N, we will pass start as 1, and stop as N + 1. Default value of step size is 1. Therefore, it will return a sequence of numbers from 1 to N. Then we can cast that sequence to a list.

Let’s see some examples,

### Example 1: With step size 1

In this example, we will create a sequence of numbers from 1 to 15.

N = 15 # Get a list of numbers from 1 to N (including N) numbers = list(range(1, N + 1)) print(numbers) # Print the type of numbers object print(type(numbers))

**Output:**

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

We created a List of numbers from 1 to N, where N is 15.

### Example 2: With step size 2

In this example, we will create a sequence of numbers from 1 to 15, with step size 2. It will keep every alternate number from 1 to 15.

N = 15 # Get a list of every alternate numbers from 1 to N (including N) numbers = list(range(1, N + 1, 2)) print(numbers)

**Output:**

[1, 3, 5, 7, 9, 11, 13, 15]

We creates a list of numbers from 1 to 15, but with step size 2. It means, list contains every alternate number from 1 to 15.

## Method 2: Using numpy.arange() function

The arange() function of NumPy module, accepts three arguments i.e. start, stop and step. It returns a numpy array of integers from **start** (inclusive) to **stop** (exclusive) by the given **step** size.

To create a list of numbers from 1 to N, we will pass **start** as 1, and **stop** as N + 1. Default value of **step** size is 1. Therefore, it will return an array of numbers from 1 to N. Then we can call the tolist() function of array, to convert it to a list of numbers.

### Example 1: With step size 1

In this example, we will create a sequence of numbers from 1 to 15.

import numpy as np N = 15 # Get a list of numbers from 1 to N (including N) numbers = np.arange(1, N+1).tolist() print(numbers)

**Output:**

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

We created a List of numbers from 1 to N, where N is 15.

### Example 2: With step size 2

In this example, we will create a sequence of numbers from 1 to 15, with step size 2. It will keep every alternate number from 1 to 15.

import numpy as np N = 15 # Get a list of every alternate numbers from 1 to N (including N) numbers = np.arange(1, N+1, 2).tolist() print(numbers)

**Output:**

[1, 3, 5, 7, 9, 11, 13, 15]

We creates a list of numbers from 1 to 15, but with step size 2. It means, list contains every alternate number from 1 to 15.

## Method 3: Using while loop

Create an empty list. Then use the while loop to iterate from 1 till N, and for each ith number in loop, append number i to the list. This way our list will have numbers from 1 till N.

Let’s see an example, in which we will create a sequence of numbers from 1 to 15.

# Create an empty list numbers = [] N = 15 i = 1 # Iterate from 1 till N while i <= N: numbers.append(i) i = i + 1 print(numbers)

**Output:**

[1, 3, 5, 7, 9, 11, 13, 15]

We creates a list of numbers from 1 to 15.

## Method 4: Using List Comprehension

To create a list of numbers from 1 to N. Pass the 1, and N+1 to the range() function. It will give a sequence of numbers from 1 to N. Then create a list from them, using List Comprehension. Let’s see an example,

N = 15 # Create a list of numbers from 1 to N numbers = [i for i in range(1, N+1)] print(numbers)

**Output:**

[1, 3, 5, 7, 9, 11, 13, 15]

We creates a list of numbers from 1 to 15.

## Summary

We learned about four different ways to create a list of numbers from 1 till N. Thanks.