Skip to main content

How to Create Lists of Numbers from 1 to N in Python

Generating a sequence of numbers from 1 to N (or a similar range) is a frequent need in Python programming.

This guide covers creating lists of numbers using the range() function, list comprehensions, for loops, while loops and the numpy.arange() method.

Creating Lists of Numbers with range() and list()

The most direct and efficient way to create a list of numbers in a range is to combine the built-in range() and list() functions:

list_of_numbers = list(range(1, 6))  # 1 to 5 (inclusive)
print(list_of_numbers) # Output: [1, 2, 3, 4, 5]
  • range(1, 6) generates a sequence of numbers starting from 1 (inclusive) up to, but not including, 6.
  • list(...) converts the range object (which is an iterator) into a list.

Customizing Start and Step

The range() function is very flexible. You can:

  • Change the starting number: range(start, stop)
  • Specify a step (increment): range(start, stop, step)
list_of_numbers = list(range(1, 10, 2)) # Start at 1, end at 9, with a step of 2
print(list_of_numbers) # Output: [1, 3, 5, 7, 9]

Creating Lists with List Comprehensions

List comprehensions offer a concise (though slightly less efficient) way to achieve the same result:

list_of_numbers = [number for number in range(1, 6)]
print(list_of_numbers) # Output: [1, 2, 3, 4, 5]
  • While it produces the same result, list(range(1,6)) is more concise and often more readable. The list comprehension isn't necessary here unless you want to perform an operation on each number as you create the list.

Floating-Point Steps with List Comprehensions

If you need a range with floating-point step, and you don't want to use the numpy, you can create a function to do this:

def custom_range(start, stop, step):
return [x * step for x in range(int(start/step), int(stop/step) + 1)]

print(custom_range(1,5, 0.5)) # Output: [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]
  • This approach is recommended if you need a floating point step and you don't want to rely on external packages.
  • This function works the same as the built-in range but accepts step as a floating point number.

Creating Lists with for Loops

You can, of course, build the list manually with a for loop:

list_of_numbers = []
for number in range(1, 6):
list_of_numbers.append(number)
print(list_of_numbers) # Output: [1, 2, 3, 4, 5]
  • This is less concise than list(range(1,6)) or the list comprehension, but it becomes useful if you need to perform more complex operations inside the loop as you build the list.

Creating Lists with while Loops

A while loop can also be used:

    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)) # Output: [1, 2, 3, 4, 5]

Creating Lists of Numbers with numpy.arange()

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

import numpy as np

list_of_numbers = np.arange(1, 10, 0.5).tolist() # Return steps of size 0.5

print(list_of_numbers)
# Output: [1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. , 6.5, 7. ,7.5, 8. , 8.5, 9. , 9.5]
  • If you are working with floating-point numbers this is a convenient alternative for creating a list of numbers.
  • This will work as long as you have numpy installed. You can install it using pip install numpy.