You are currently viewing Convert Python Range to List

How to convert Python range to list? You can create a list using range() function in several ways. The range() function in Python returns a sequence of numbers based on the specified start, stop, and step values. The default behavior of the range() function is to start from 0 and increment by 1. The stop value is exclusive, meaning the sequence stops before reaching the specified stop number. To convert a range object to a list in Python, you can use the list() constructor, passing the range object as an argument. This will create a new list containing all the values from the range object.

Advertisements

You can convert a range of sequence to a list in Python using many ways, for example, by using range(), * unpacking operator, list(), list comprehension, for loop, and extend() functions. In this article, I will explain how to convert a range object to a list by using all these functions with examples.

1. Quick Examples of Range to a List

If you are in a hurry, below are some quick examples of the range to list.


# Quick examples of range to a list

# Example 1: Range to list 
# Using * unpacking Operator
myrange = range(5, 10, 1)
mylist = [*myrange]

# Example 2: Range to List using list()
mylist = list(range(10, 20))

# Example 3: Using list() function
mylist = range(10, 20, 1)

# Example 4: Using list comprehension
mylist = [x for x in range(10, 21)]

# Example 5: Range to list 
# Using for loop
myrange = range(5, 15, 2)
my_list = []
for x in myrange:
    my_list.append(x)

# Example 6: Using extend() function
myrange = range(5, 20, 3)
my_list = []
my_list.extend(myrange)

# Example 7: Create an empty list
my_list = []
start, end = 5, 10  
# Check if start value
# smaller than end value
if start < end:
    my_list.extend(range(start, end))
    my_list.append(end)  

2. Syntax of range() Function

Following is the syntax of the Python range() function.


# Syntax of range() function
range(start, stop, step)

2.1 Parameter of range()

  • start (optional) – Specifies the starting value of the sequence (inclusive). If not provided, it defaults to 0.
  • stop (required) – Specifies the ending value of the sequence (exclusive). The sequence will include numbers up to before the specified stop value.
  • step (optional) – Specifies the increment (or decrement) between each number in the sequence. If not provided, it defaults to 1.

3. Range to List Using * unpacking Operator

You can convert the range object to a list using the * unpacking operator. For example, you can create a range object using range(5, 10, 1), which generates a sequence of numbers from 5 to 9 (inclusive) with a step of 1. Then, you unpack the range object using the * operator within the list comprehension [*myrange], which expands the individual elements of the range object. Finally, you assign the resulting list to the variable mylist.


# Range to list 
# Using * unpacking Operator
myrange = range(5, 10, 1)
print("Sequence of range:", myrange)
mylist = [*myrange]
print("After converting the list from range:", mylist)

Yields below output.

Python list range

4. Range to List Using list()

You can create a list within the range of 1019 (inclusive) in Python using the list()function, you can pass the range() function as an argument to the list() function. For example, the range(10, 20) generates a sequence of numbers starting from 10 and ending at 19 (inclusive). The list() function converts this range object into a list.

The range() function generates a sequence of numbers up to, but not including, the specified stop value. To include the stop value in the list, you need to specify it as 20 instead of 19 in the range() function.


# Convert range object to list using list()
myrange = range(10, 20, 1)
print("Sequence of range:", myrange)
mylist = list(myrange)
print("After converting the list from range:", mylist)

Yields below output.

Python list range

5. Range to List Using List Comprehension

You can create a list within the range of 10-20 (inclusive) in Python, you can use a list comprehension. For example, the list comprehension [x for x in range(10, 21)] generates a list by iterating over the range of numbers from 10 to 20 (inclusive). Each number is assigned to the variable x, and it is added to the list. The resulting list contains the numbers [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20].

Note that the range used in the range() function is from 10 to 21, where 21 is not included in the generated list.


# Convert range to list using list comprehension 
mylist = [x for x in range(10, 21)]
print("The range to list is:",mylist)

# Output: 
# The range to list is: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

6. Range to List Using For Loop

You can also convert a range object to a list using a for loop in Python. For example, you can create a range object using range(5, 15, 2), which generates a sequence of numbers starting from 5 and ending at 14 (inclusive) with a step of 2. Then, you can iterate over each element of the range object using the for loop. Within the loop, you append each element x to the my_list using the append() function.


# Range to list 
# Using for loop
myrange = range(5, 15, 2)
my_list = []

for x in myrange:
    my_list.append(x)
print("The range to list is:", my_list)

# Output:
# The range to list is: [5, 7, 9, 11, 13] 

7. Using extend() Function

You can also use extend() function to unpack the result of the range() function and add its elements to an existing list.

In the below example, the range object myrange is created using range(5, 20, 3), which generates a sequence of numbers starting from 5 and ending at 19 (inclusive) with a step of 3. The extend() function is then used on the empty list my_list to add all the elements from the range object.


# Using extend() function to convert
range object to list
myrange = range(5, 20, 3)
my_list = []
my_list.extend(myrange)
print("The range to list is:", my_list)

# Output:
# The range to list is: [5, 8, 11, 14, 17]

Alternatively, you can use extend() function with append() function to convert a range object to a list. First, you can create an empty list and then use the extend() function to add the elements from the range to the list. Additionally, it appends the end value to the list if the start value is smaller than the end value.

In the below example, you create an empty list my_list. Then, you use the extend() function to add the elements from the range(start, end) to the list. The range is generated from the start value of 5 (inclusive) to the end value of 10 (exclusive). Since the start value is smaller than the end value, the range includes numbers from 5 to 9. Finally, you append the end value of 10 to the list using the append() function.


# Create an empty list
my_list = []
start, end = 5, 10
  
# Check if start value is smaller than end value
if start < end:
    my_list.extend(range(start, end))
    my_list.append(end)    
print("The range to list is:", my_list)

# Output:
# The range to list is: [5, 6, 7, 8, 9, 10]

8. Conclusion

In this article, I have explained the Python range() function syntax, parameters, and usage. Also explained using various functionss along with the range() function how we can convert the range of sequence to a list with multiple examples.

Happy Learning !!