Print range list Python

Python Range object is an iterable that creates elements lazily. In other words all the elements of the range are not stored in memory. Only that element during an iteration is returned by the range object. Python List is not like that. All the elements of a list are stored in memory.

Python Range can save memory resources while list can offer speed. So, based on the requirement, you may need to convert a Python range object to a list.

To convert a Python Range to Python List, use list() constructor, with range object passed as argument. list() constructor returns a list generated using the range. Or you can use a for loop to append each item of range to list.

In this tutorial, we will learn how to convert a Python range object to a list, with the help of list() constructor and for loop.

Example 1 – Python Range to List using list()

In this example, we will create a range object and then convert this range object to list.

Python Program

range_1 = range(2, 20, 3)
list_1 = list(range_1)
print(list_1)
Try Online

Output

[2, 5, 8, 11, 14, 17]

Example 2 – Python Range to List using For Loop

You can also use Python For Loop, to iterate over each element of range, and then append the item to list.

In this example, we will create a range object and then convert this range object to list.

Python Program

range_1 = range(2, 20, 3)

list_1 = list()
for x in range_1 :
    list_1.append(x)

print(list_1)
Try Online

Output

[2, 5, 8, 11, 14, 17]

Conclusion

In this Python Tutorial, we learned how to convert a Python range to Python List using list() constructor or for loop.

Python range() is a built-in function available with Python from Python(3.x), and it gives a sequence of numbers based on the start and stop index given. In case the start index is not given, the index is considered as 0, and it will increment the value by 1 till the stop index.

For example range(5) will output you values 0,1,2,3,4 .The Python range()is a very useful command and mostly used when you have to iterate using for loop.

In this tutorial, you will learn:

Syntax

range(start, stop, step)

Parameters

  • start: (optional) The start index is an integer, and if not given, the default value is 0.
  • stop: The stop index decides the value at which the range function has to stop. It is a mandatory input to range function. The last value will be always 1 less than the stop value.
  • step: (optional).The step value is the number by which the next number is range has to be incremented, by default, it is 1.

Return value:

The return value is a sequence of numbers from the given start to stop index.

Python range() Function and history

Python range() has been introduced from python version 3, before that xrange() was the function.

Both range and xrange() are used to produce a sequence of numbers.

Following are the difference between range and xrange():

range()xrange()The range() gives the sequence of numbers and returns a list of numbers.The xrange() function gives a generator object that needs to be looped in a for-loop to get the values.The range() returns a list.xrange() returns a generator object.The range() method uses more memory as the list returned has to be stored in comparison to xrange().As xrange() returns a generator object, it does not give values instantly and has to be used inside for-loop to get the values.The usage of memory is more hence the code execution is slow when working on a huge set of data.The code execution is faster using xrange().

Using range()

This example shows how to print the values from 0-9 using range().

The value used in range is 10, so the output is 0 1 2 3 4 5 6 7 8 9

Since the start is not given the start is considered as 0 and the last value is given till 9. The last value is always 1 less than the given value i.e. stop-1.

for i in range(10):
    print(i, end =" ")

Output:

0 1 2 3 4 5 6 7 8 9

Using start and stop in range()

In the code, the start value is 3, and stop value is 10. Here the start index is 3, so the sequence of numbers will start from 3 till the stop value. The last value in the sequence will be 1 less than the stop value 10-1 = 9.

for i in range(3, 10):
    print(i, end =" ")

Output:

3 4 5 6 7 8 9

Using start, stop and step

The start value is 3, so the sequence of numbers will start at 3. The stop value is 10, so the sequence of numbers will stop at (10-1) i.e 9. The step is 2, so each value in the sequence will be incremented by 2. If the step value is not given, the value for step defaults to 1.

for i in range(3, 10, 2):
    print(i, end =" ")

Output:

3 5 7 9

So far, we have seen how range() function gives the incremented value for the stop value given. Let us now try an example to get the decremented value in the range given.

Incrementing the values in range using a positive step.

The parameter step in range() can be used to increment /decrement the values. By default, it is a positive value 1. So it will always give incremented values.

The step value has to be positive incase you want to want incremented values as ouput.

for i in range(1, 30, 5):
    print(i, end =" ")

Output:

1 6 11 16 21 26

Reverse Range: Decrementing the values using negative step.

The parameter step with negative value in range() can be used to get decremented values. In the example below the step value is negative so the output will be in decremented from the range value given.

for i in range(15, 5, -1):
    print(i, end =" ")

Output:

for i in range(10):
    print(i, end =" ")
0

The start value is 15, the stop value is 5 and the step value is negative number i.e -1. With above inputs range() function will decrement the value from 15 onwards till it reaches the stop value , but here the difference is the last value will be stop + 1.

Using floating numbers in Python range()

Let us now work on the range() using floating-point numbers.

Example:

for i in range(10):
    print(i, end =" ")
1

In above example we have used stop value as 10.5.

The output is :

for i in range(10):
    print(i, end =" ")
2

Python gives an error as the range() function does not support floating-point numbers for start, stop and step.

Using for-loop with Python range()

In this example we will use a array of numbers and, let us see how to use the iterate the array inside for-loop using range()

Example:

for i in range(10):
    print(i, end =" ")
3

Output:

for i in range(10):
    print(i, end =" ")
4

In above example we have used len(arr_list) as the stop value. The for loop will iterate till the stop value i.e the length of the array and that will be 4, as we have four items in the arr_list. The start value will be 0 and step will be 1.So the values will start from 0 and will stop at 3 i.e length of array -1 meaning 4 -1 = 3.

Using Python range() as a list

In this example will see how to make use of the output from range as a list.

Example:

for i in range(10):
    print(i, end =" ")
5

Output:

for i in range(10):
    print(i, end =" ")
6

You can see the output is a list format. It was not necessary to loop the range() and using list() method we could directly convert the output from range to list format.

Using characters in python range()

So far, we have used integers in python range(). We have also seen that floating-point numbers are not supported in python range. Let us try and see the output as to what happens when we use characters.

Example:

for i in range(10):
    print(i, end =" ")
7

Output:

for i in range(10):
    print(i, end =" ")
8

It throws an error saying a string cannot be interpreted as an integer.

To get the list of the alphabets you can customize the code and get the desired outputas shown below:

Example:

for i in range(10):
    print(i, end =" ")
9

Output:

0 1 2 3 4 5 6 7 8 9
0

How to Access Range Elements

You can make use of a for-loop to get the values from the range or use the index to access the elements from range().

Using for-loop

Example:

0 1 2 3 4 5 6 7 8 9
1

Output:

0 1 2 3 4 5 6 7 8 9
2

Using index

The index is used with range to get the value available at that position. If the range value is 5, to get the startvalue, you can use range(5)[0] and the next value range(5)[1] and so on.

Example:

0 1 2 3 4 5 6 7 8 9
3

Output:

0 1 2 3 4 5 6 7 8 9
3

Using list()

This method will print all the elements from the range(). Using list() it will return the elements from range() in list format.

Example:

for i in range(10):
    print(i, end =" ")
5

Output:

0 1 2 3 4 5 6 7 8 9
6

It gives the list output for the range given.

Example: Get even numbers using range()

Using range() will get the list of even numbers in the range given as input. The parameters for range() are, start is 2, stop is 20, and step is 2, so the values will be incremented by 2 and will give even numbers till stop-2.

Example:

0 1 2 3 4 5 6 7 8 9
7

Output:

0 1 2 3 4 5 6 7 8 9
8

Merging two-range() outputs

In this example will concatenate 2 range() functions with the help of itertools module chain() function.

Example:

0 1 2 3 4 5 6 7 8 9
9

Output:

for i in range(3, 10):
    print(i, end =" ")
0

Using range() With NumPy

The NumPy module has arange() function that works and gives similar output like range(). The arrange() takes in the same parameters like range().

Syntax:

for i in range(3, 10):
    print(i, end =" ")
1

To work with NumPy follow the steps given below.

Step 1: Import NumPy module

for i in range(3, 10):
    print(i, end =" ")
2

Incase while execution, it gives an error saying numpy module not found, you need to install the module as shown in step 2.

Step 2: Install NumPy

for i in range(3, 10):
    print(i, end =" ")
3

Step 3: Working Example of arange() using NumPy

for i in range(3, 10):
    print(i, end =" ")
4

Output:

0 1 2 3 4 5 6 7 8 9

Floating point numbers using NumPy arange()

It is not possible to get the floating point sequence using range(), but it is possible using NumPy arange().

Example:

The range that we want is from 0.5 to 1.5. The value will be increment by 0.2.

for i in range(3, 10):
    print(i, end =" ")
6

Output:

for i in range(3, 10):
    print(i, end =" ")
7

The output we get is a little weird,some of the float numbers are shown with 16 decimal places. This happens because of the complexity of storing decimal floating numbers into binary format. You can also round the values if required and limit them to the decimal places you need.

How do you print a range in 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:.

Does range () return a list?

The range() function, on the other hand, returns a list or sequence of numbers and consumes more memory than xrange() . Since the range() function only stores the start, stop, and step values, it consumes less amount of memory irrespective of the range it represents when compared to a list or tuple.

What does list range ()) do in Python?

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.

Does range () Return list or array?

Python range() function 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.