Python Range to List

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

</>
Copy
range_1 = range(2, 20, 3)
list_1 = list(range_1)
print(list_1)

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

</>
Copy
range_1 = range(2, 20, 3)

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

print(list_1)

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.