Skip to content

Python convert range to list | Example code

  • by

You can just construct a list from the range object.

Example convert range to list in Python

Simple python example code.

Generate number 0 to 1 using range

Using 0 to 11 range to create a list 0 to 11.

my_list = list(range(0, 11))

print(my_list)

Output:

Python convert range to list

Use Python For Loop, to iterate over each element of the range

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]

Create a list with numbers between 2 values

In Python 3.x range is an iterator. So, you need to convert it to a list:

my_list = list(range(11, 17))

print(my_list)

Output: [11, 12, 13, 14, 15, 16]

Do comment if you have any doubts and suggestions on this Python list topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading