Python – Create list of numbers with given range



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 do this task.

Approach #1 : Naive Approach

A naive method to create list within a given range is to first create an empty list and append successor of each integer in every iteration of for loop.

 

 

# Python3 Program to Create list 
# with integers within given range 
 
def createList(r1, r2):
 
    # Testing if range r1 and r2 
    # are equal
    if (r1 == r2):
        return r1
 
    else:
 
        # Create empty list
        res = []
 
        # loop to append successors to 
        # list until r2 is reached.
        while(r1 < r2+1 ):
             
            res.append(r1)
            r1 += 1
        return res
     
# Driver Code
r1, r2 = -1, 1
print(createList(r1, r2))
Output:
[-1, 0, 1]

Approach #2 : List comprehension

We can also use list comprehension for the purpose. Just iterate ‘item’ in a for loop from r1 to r2 and return all ‘item’ as list. This will be a simple one liner code.

# Python3 Program to Create list 
# with integers within given range 
 
def createList(r1, r2):
    return [item for item in range(r1, r2+1)]
     
# Driver Code
r1, r2 = -1, 1
print(createList(r1, r2))
Output:
[-1, 0, 1]

Approach #3 : using Python range()

Python comes with a direct function range() which creates a sequence of numbers from start to stop values and print each item in the sequence. We use range() with r1 and r2 and then convert the sequence into list.

# Python3 Program to Create list 
# with integers within given range 
 
def createList(r1, r2):
    return list(range(r1, r2+1))
     
# Driver Code
r1, r2 = -1, 1
print(createList(r1, r2))
Output:
[-1, 0, 1]

Approach #4 : Using numpy.arange()

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

Last Updated on August 30, 2021 by admin

Leave a Reply

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

Recommended Blogs