Create a List from 1 to 100 in Python

Create a list from 1 to 100 in Python

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.

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

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.

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

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,

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

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.

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *