Create a List of Numbers from 1 to N in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
5 min

banner

# Table of Contents

  1. Create a list of numbers from 1 to N in Python
  2. Create a list of numbers from 1 to N using a list comprehension
  3. Using a floating-point number for the step with numpy
  4. Create a list of numbers from 1 to N using a list comprehension
  5. Create a list of numbers from 1 to N using a for loop
  6. Create a list of numbers from 1 to N using a while loop

# Create a list of numbers from 1 to N in Python

To create a list of numbers from 1 to N:

  1. Use the range() class to create a range object from 1 to N.
  2. Use the list() class to convert the range object to a list.
  3. The new list will contain the numbers in the specified range.
main.py
list_of_numbers = list(range(1, 6)) print(list_of_numbers) # ๐Ÿ‘‰๏ธ [1, 2, 3, 4, 5] list_of_numbers = list(range(1, 10, 2)) print(list_of_numbers) # ๐Ÿ‘‰๏ธ [1, 3, 5, 7, 9]

create list of numbers from 1 to n

The code for this article is available on GitHub

The range() class is commonly used for looping a specific number of times in for loops and takes the following arguments:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)

If you only pass a single argument to the range() constructor, it is considered to be the value for the stop parameter.

main.py
list_of_numbers = list(range(5)) # ๐Ÿ‘‡๏ธ [0, 1, 2, 3, 4] print(list_of_numbers)
The example shows that if the start argument is omitted, it defaults to 0 and if the step argument is omitted, it defaults to 1.

If values for the start and stop parameters are provided, the start value is inclusive, whereas the stop value is exclusive.

main.py
list_of_numbers = list(range(1, 5)) # ๐Ÿ‘‡๏ธ [1, 2, 3, 4] print(list_of_numbers)

If you need to specify a step, pass a third argument to the range() class.

main.py
list_of_numbers = list(range(1, 10, 2)) print(list_of_numbers) # ๐Ÿ‘‰๏ธ [1, 3, 5, 7, 9]

The list contains every second number starting from 1 to 10.

The step argument can only be set to an integer.

# Create a list of numbers from 1 to N using a list comprehension

You can also use a list comprehension to create a list of numbers from 1 to N.

main.py
start = 1 stop = 5 step = 0.5 list_of_numbers = [ x * step for x in range(2*start, 2*stop + 1) ] # ๐Ÿ‘‡๏ธ [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0] print(list_of_numbers)

create list of numbers from 1 to n using list comprehension

The code for this article is available on GitHub
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

The list in the example starts at 1 and goes to 5 (inclusive) in increments of 0.5.

You can update the start, stop and setup values.

This approach is useful when you have a step value that is not an integer because the range() class only supports integer values.

# Using a floating-point number for the step with numpy

If you need to use a floating-point number for the step, use the numpy.arange() method.

main.py
import numpy as np list_of_numbers = np.arange(1, 10, 0.5).tolist() # ๐Ÿ‘‡๏ธ [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, # 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5] print(list_of_numbers)

using floating point number for the step with numpy

The code for this article is available on GitHub

The numpy.arange() method returns an array of evenly spaced values within the given interval.

We used the tolist() method to convert the array to a list.

If you need to install the NumPy package, run the following command.

shell
pip install numpy # ๐Ÿ‘‡๏ธ or pip3 pip3 install numpy

You can omit the step argument to get a list of values from 1 to N in increments of 1.

main.py
import numpy as np list_of_numbers = np.arange(1, 11).tolist() # ๐Ÿ‘‡๏ธ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(list_of_numbers)

The numpy.arange() method is very similar to the range() class but allows for step values that are floating-point numbers.

# Create a list of numbers from 1 to N using a list comprehension

You can also use a list comprehension to create a list of numbers from 1 to N.

main.py
list_of_numbers = [number for number in range(1, 6)] print(list_of_numbers) # ๐Ÿ‘‰๏ธ [1, 2, 3, 4, 5]
The code for this article is available on GitHub
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we return the current number.

The new list contains the numbers in the range object.

However, this isn't necessary unless you need to perform some operation for every number in the range object.

# Create a list of numbers from 1 to N using a for loop

The same can be achieved using a simple for loop.

main.py
list_of_numbers = [] for number in range(1, 6): list_of_numbers.append(number) print(list_of_numbers) # ๐Ÿ‘‰๏ธ [1, 2, 3, 4, 5]
The code for this article is available on GitHub

We used a for loop to iterate over the range object.

On each iteration, we use the list.append() method to append the current number to a list.

The list.append() method adds an item to the end of the list.

main.py
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

Using the list() class to convert the range object to a list should be sufficient unless you have to perform some operation for every number in the range object.

You can also define a reusable function that creates a list of numbers in a for loop.

main.py
def get_range(n): list_of_numbers = [] for number in range(1, n + 1): list_of_numbers.append(number) return list_of_numbers print(get_range(6)) # ๐Ÿ‘‰๏ธ [1, 2, 3, 4, 5, 6] print(get_range(7)) # ๐Ÿ‘‰๏ธ [1, 2, 3, 4, 5, 6, 7]

The function takes n as a parameter and creates a list of numbers from 1 to n.

# Create a list of numbers from 1 to N using a while loop

You can also use a while loop to create a list of numbers from 1 to N.

main.py
def get_range(n): list_of_numbers = [] start = 1 while start < n: list_of_numbers.append(start) start += 1 return list_of_numbers print(get_range(6)) # ๐Ÿ‘‰๏ธ [1, 2, 3, 4, 5] print(get_range(7)) # ๐Ÿ‘‰๏ธ [1, 2, 3, 4, 5, 6]

create list of numbers from 1 to n using while loop

The code for this article is available on GitHub

The get_range function takes n as a parameter and creates a list of numbers from 1 to n.

On each iteration, we append the current start value to the list and increment it.

Once the start value is equal to or greater than n, the condition is no longer met and the while loop exits.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev