How to use numpy.arange() In Python
Python numpy.arange() returns a list with evenly spaced elements as per the interval. Here we set the interval as 1 according to our need to get the desired output.
Python3
# Python3 Program to Create list # with integers within given range import numpy as np def createList(r1, r2): return np.arange(r1, r2 + 1 , 1 ) # Driver Code r1, r2 = - 1 , 1 print (createList(r1, r2)) |
Output:
[-1, 0, 1]
Python | Create list of numbers with given range
Given two numbers r1 and r2 (which defines the range), write a Python program to create a list with the given range (inclusive).
Examples:
Input : r1 = -1, r2 = 1 Output : [-1, 0, 1] Input : r1 = 5, r2 = 9 Output : [5, 6, 7, 8, 9]
Let’s discuss a few approaches to Creating a list of numbers with a given range in Python.