How do you range a list in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Often times we want to create a list containing a continuous value like, in a range of 100-200. Let’s discuss how to create a list using the range[] function.

    Will this work ?

    My_list = [range[10, 20, 1]]

    print[My_list]

    Output :


    As we can see in the output, the result is not exactly what we were expecting because Python does not unpack the result of the range[] function.

    Code #1: We can use argument-unpacking operator i.e. *.

    My_list = [*range[10, 21, 1]]

    print[My_list]

    Output :


    As we can see in the output, the argument-unpacking operator has successfully unpacked the result of the range function.
     
    Code #2 : We can use the extend[] function to unpack the result of range function.

    My_list = []

    start, end = 10, 20

    if start < end:

        My_list.extend[range[start, end]]

        My_list.append[end]

    print[My_list]

    Output :


    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]

    Can you do range list in Python?

    In Python, you can generate a series of numbers with the built-in function range[] .

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

    # Create a list in a range of 10-20. My_list = [ range [ 10 , 20 , 1 ]] # Print the list. print [My_list].
    # Create a list in a range of 10-20. My_list = [ * range [ 10 , 21 , 1 ]] # Print the list. print [My_list].

    How does list range work in Python?

    The range[] function is used to generate a sequence of numbers over time. At its simplest, it accepts an integer and returns a range object [a type of iterable]. In Python 2, the range[] returns a list which is not very efficient to handle large data. [optional] Starting point of the sequence.

    How do you find the range of a list?

    The range is the difference between the smallest and highest numbers in a list or set. To find the range, first put all the numbers in order. Then subtract [take away] the lowest number from the highest.

    Chủ Đề