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.
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.
Frequently Asked:
- Remove consecutive duplicates in a Python List
- Sort a List in descending / reverse order in Python
- Python: check if two lists are equal or not ( covers both Ordered & Unordered lists)
- Convert a list of tuples to a list in Python
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.