How do you create a range in a list in python?

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

def createList(r1, r2):

 if (r1 == r2):

  return r1

 else:

  res = []

  while(r1 < r2+1 ):

   res.append(r1)

   r1 += 1

  return res

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

def createList(r1, r2):

    return [item for item in range(r1, r2+1)]

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

def createList(r1, r2):

    return list(range(r1, r2+1))

r1, r2 = -1, 1

print(createList(r1, r2))

Output:

[-1, 0, 1]

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

import numpy as np

def createList(r1, r2):

    return np.arange(r1, r2+1, 1)

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]

How do you add a range to a list in Python?

Use the list. extend() method to append a range to a list in Python, e.g. my_list. extend(range(2)) . The extend method takes an iterable (such as a range) and extends the list by appending all of the items from the iterable.

How do you create a range in Python?

The below steps show how to use the range() function in Python..
Pass start and stop values to range() For example, range(0, 6) . ... .
Pass the step value to range() The step Specify the increment. ... .
Use for loop to access each number. Use for loop to iterate and access a sequence of numbers returned by a range() ..

How does range () function of Python generate a list?

The Python range() function returns the sequence of the given number between the given range. The most common use of it is to iterate sequence type (Python range() List, string, etc. ) with for and while loop using Python.

How do you print a range of a list in Python?

Python: range() function.
Version: ... .
Syntax: range(stop) range(start, stop[, step]).
Parameter: ... .
Return value: ... .
Example-1: Python range() function # empty range print(list(range(0))) # using range(stop) print(list(range(12))) # using range(start, stop) print(list(range(1, 15))) ... .
Pictorial Presentation:.
Pictorial Presentation:.