Table of Contents
In this article, we will see how to create a list from 1 to 100 in Python.
Ways to create a list from 1 to 100 in Python
A list is an object that contains a sequence of elements in Python.
We will discuss how to create a list from 1 to 100 in Python.
Using the range()
function to create a list from 1 to 100 in Python
In Python, we can use the range()
function to create an iterator sequence between two endpoints. We can use this function to create a list from 1 to 100 in Python.
The function accepts three parameters start
, stop
, and step
. The start
parameter mentions the starting number of the iterator and the ending point is specified in the stop
parameter. We use the step
parameter to specify the step increment between two consecutive numbers. By default, the step
parameter has a value of 1.
Since the range()
function returns an iterator, we need to convert it to a list. For this, we will use the list()
constructor.
See the code below.
1 2 3 4 |
lst = list(range(1,101)) print(lst) |
Output:
In the above example, we do not mention the step
parameter. Note that we have to specify the value for the ending point as 101. This is because the last value is not included in the sequence.
The range()
function works a little differently for users working with Python 2. In this version, the final result is already returned in a list so we do not need to perform any explicit conversion.
Using the numpy.arange()
function to create a list from 1 to 100 in Python
The numpy.arange()
function is similar to the previous method. It also takes three parameters start
, stop
, and step
, and returns a sequence of numbers based on the value of these parameters.
However, the final result in this function is returned in a numpy
array. So we need to convert this array to a list which can be done by using the tolist()
function. This function is used to return the elements of an array in a list.
See the code below.
1 2 3 4 5 |
import numpy as np lst = np.arange(1,101).tolist() print(lst) |
Output:
Further reading:
Using the for
loop with range() to create a list from 1 to 100 in Python
This method is just on a need-to-know basis as it is rarely used. We will essentially iterate using a for
loop from 1 to 100 and append the value in every iteration to a list. The only advantage of using this method is that we can perform some calculations for the value if required in every iteration.
For example,
1 2 3 4 5 6 |
lst = [] for i in range(1,101): lst.append(i) print(lst) |
Output:
Another way to use this is via list comprehension. List comprehension is an elegant way to create lists using the for
loop in one line.
We can use it to create a list from 1 to 100 in Python.
1 2 3 4 |
lst = [i for i in range(1,101)] print(lst) |
Output:
Conclusion
To conclude, we discussed how to create a list from 1 to 100 in Python. The first method involves the range()
function to create a sequence and convert it to a list using the list()
function. The numpy.arange()
function creates the sequence in an array, and we can convert this to a list with the tolist()
function. We can also use the for
loop for this. If we intend to use the for
loop method, one should focus on list comprehension to make the code faster.