Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Create list of numbers with given range

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 07 Jan, 2023
Improve Article
Save Article

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

Naive Approach using a loop

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

Python3




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

Using 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




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

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




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

Using itertools:

You can also use the range function in combination with the itertools module’s chain function to create a list of numbers with a given range. This can be done as follows:

Python3




# Python3 Program to Create list
# with integers within given range
import itertools
r1=-5
r2=5
#using the chain function from the itertools module
numbers = list(itertools.chain(range(r1, r2+1)))
print(numbers)
#This code is contributed by Edula Vinay Kumar Reddy


Output

[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]

This will create a list of numbers from r1 to r2 inclusive by chaining the elements in the range together using the chain function from the itertools module.

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




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

Using numpy to create list of numbers with given range

Here we are creating a list of numbers from a given range with the defined increment.

Python3




import numpy as np
 
def fun(start, end, step):
    num = np.linspace(start, end,(end-start)
                      *int(1/step)+1).tolist()
    return [round(i, 2) for i in num]
     
 
print(fun(1,5,0.5))


Output:

[1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]

Using Recursion

Another approach to create a list of numbers within a given range is to use recursion. This involves defining a recursive function that takes in the current range and the list to be returned, and then appending the current range to the list and calling the function again with the next range until the end of the range is reached.

Python3




def create_list(r1, r2, lst):
    # Base case: if r1 is equal to r2, return the list
    if r1 == r2+1:
        return lst
    # Otherwise, append r1 to the list and call the function again with r1 + 1
    else:
        lst.append(r1)
        return create_list(r1 + 1, r2, lst)
 
# Test the function
print(create_list(5, 9, []))  # [5, 6, 7, 8, 9]
print(create_list(-1, 1, [])) # [-1, 0, 1]
#This code is contributed by Edula Vinay Kumar Reddy


Output

[5, 6, 7, 8, 9]
[-1, 0, 1]

This approach has a time complexity of O(n) and an auxiliary space of O(n), where n is the number of elements in the list.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!