Create a List with numbers between two values in Python

In this article, we will discuss how to create a Python List of numbers between two values.

Table Of Contents

Introduction

Suppose first value is 20, and second value is 30, and we want to create a list containing numbers from first to second value. Like this,

[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

There are different ways to create a list containing numbers from A to B. Where, both A and B are numbers. 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 A to B, we will pass start as A, and stop as B. Default value of step size is 1. Therefore, it will return a sequence of numbers from A till B-1. Then we can cast that sequence to a list.

Important Point: Numbers in list will be from A till B-1. Number B is excluded. If you want to include B also, then pass B+1 in the range() function.

Let’s see an examples, where we will create a sequence of numbers from 20 to 30 (excluding 30).

a = 20
b = 30

# Get a list of numbers from a to b (including b)
numbers = list(range(a, b))

print(numbers)

Output:

[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

We created a List of numbers from 20 till 30. Where, 30 is not including in the list.

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 A to B, we will pass start as A, and stop as B. Default value of step size is 1. Therefore, it will return an array of numbers from A to B-1. Then we can call the tolist() function of array, to convert it to a list of numbers.

Important Point: Numbers in list will be from A till B-1. Number B is excluded. If you want to include B also, then pass B+1 in the range() function.

Let’s see an examples, where we will create a sequence of numbers from 20 to 30 (excluding 30).

import numpy as np

a = 20
b = 30

# Get a list of numbers from a to b (including b)
numbers = np.arange(a, b).tolist()

print(numbers)

Output:

[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

We created a List of numbers from a to b (excluding b). Where a is 20, and b is 30.

Method 3: Using while loop

Create an empty list. Then use the while loop to iterate from a till b, and for each ith number in loop, append number i to the list. This way our list will have numbers from a till b-1. Let’s see an example, in which we will create a sequence of numbers from a to b. Where a is 20, and b is 30.

# Create an empty list
numbers = []

a = 20
b = 30

# Iterate from a to b
while a < b:
    numbers.append(a)
    a = a + 1

print(numbers)

Output:

[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

We created a List of numbers from a to b (excluding b). Where a is 20, and b is 30.

Method 4: Using List Comprehension

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

a = 20
b = 30

# Create a list of numbers from a till b-1
numbers = [i for i in range(a, b)]

print(numbers)

Output:

[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

We created a List of numbers from a to b (excluding b). Where a is 20, and b is 30.

Summary

We learned about four different ways to create a list of numbers from a to b (excluding b). Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top