How to convert the range of values to a list in Python? There are multiple ways to convert the range to a list in Python for example you can use the list() function, this function takes the set as an argument and returns the list. Besides this you can also use the [], append(), and extend() methods.
The following are methods to convert the range to a list in Python:
- Method 1: Use the list() function with a range as a parameter to get the list.
- Method 2: Use [] and unpack operator (*)
- Method 3: Use append() or insert() with for loop to create a list from range.
- Method 4: Using extend() to add elements from range to list
1. Quick Examples of Converting Range to List
Following are quick examples of converting a range of values to a List.
# Quick examples of convert range to list
values=range(100,110,3)
# Using list() to convert range
list_range = list(values)
print(list_range)
# Using [] and unpack operator
list_range = [*values]
print(list_range)
# Using append()
list_range = []
for i in values:
list_range.append(i)
print(list_range)
list_range = []
# Using extend()
list_range.extend(values)
print(list_range)
2. What is Range & List in Python?
The range
in Python generates a sequence of numbers, starting from 0 by default, increments by 1 (by default), and stops before a specified number. It can be used in for loops or to create a list of numbers. Syntax: range(stop)
, range(start, stop)
, range(start, stop, step)
.
Whereas the list is an ordered collection of elements, which can be of different data types (such as integer, float, string, etc.). Lists are mutable, meaning their elements can be changed.
When you required a list with a sequence of values we typically use the range() and convert the range to a list.
3. Using list() to Convert Range to List
The list() is a built-in Python function that converts a range of values to a list., this function takes the iterable like set, list, and tuple as a parameter and converts it to a list.
3.1 Syntax
Following is the syntax of the range() with list() function.
# Syntax
list(range(start,stop,step))
3.2 Convert Range to List Example
Let’s create a range of sequence values that start at 10, and end at 15 and store it to a variable, use this range variable as an argument to list() function to convert to a list. This function takes each element from the range and added to the list in the same order.
# Create a range
values = range(10,15)
# Convert range to list
list_range = list(values)
print(list_range)
# Output:
# [10, 11, 12, 12, 14]
After converting the range to a list, the list holds elements starting from 10 to 15.
Let’s see another example by specifying the step parameter in the range() function. Here, we specified step as 4, so after 10, it will directly jump to 14 and then 18, so the list holds 10, 14, and 18 elements.
# Create range
values=range(10,20,4)
# Pass range of values to list
list_range = list(values)
print(list_range)
# Output:
# [10, 14, 18]
4. Using [] with * operator
Here, we will unpack the range of values using * operator and pass the range() inside the list – []. By this way, the range of values is stored in a list.
Let’s see how to use [] with * operator to convert range() to List.
[*range(start,stop,step)]
4.1 Example
Consider the range() with start and stop parameters and convert the range of values to a list.
values=range(100,110)
# Pass range of values to list
list_range = [*values]
print(list_range)
# Output:
# [100, 101, 102, 103, 104, 105, 106, 107, 108, 109]
We specified the start parameter as 100 and the stop as 110. After that, we passed this to the list. Now, the list holds elements starting from 100 to 109.
5. Using list.append()
The list.append() method can also be used to convert elements from range to list in Python. If we pass a range of values to this method, the values in the range are stored in a list as a single element, so we need to use the for loop to iterate a range of values and use the append() method inside the loop to add elements to the list.
values=range(1,100,50)
# Using append() to convert to list
list_range = []
for i in values:
list_range.append(i)
print(list_range)
# Output:
# [1, 51]
We specified start and stop as 1 and 100. We are incrementing by 50. So the list had 1 and 51.
6. Using list.extend()
The list.extend() method is used to add elements at the end of the list. If we pass the range of values to this method, the values in the range are stored in a list. In this case, no need to use the loop. Here, is an example.
values=range(100,110)
# Using extend()
list_range = []
list_range.extend(values)
print(list_range)
# Output:
# [100, 101, 102, 103, 104, 105, 106, 107, 108, 109]
7. Conclusion
In this article, you have learned how to convert range to list in Python by using the list() function with a range as a parameter. By using [] and unpack operator (*), append(), insert() and finally using the extend.
Related Articles
- Python Join Lists (Two or Multiple Lists)
- Get Index of Min of List in Python
- Average of List in Python ( 7 Examples)
- Sum of Elements in the List in Python
- Python Find Item Index in List
- Python Flatten List of Lists with Example
- Python – range() Inclusive Values
- Reverse a Range in Python with Examples
- Python range() with float values