Skip to content

How to Create a List with a Range of Numbers in Python

Learn how to generate numeric lists in Python using range(), list comprehensions, unpacking operators, and NumPy for integer sequences, custom logic, and high-performance arrays.

When working with loops, generating test data, or initializing numeric arrays, you often need to create a list containing a sequence of numbers (e.g., [0, 1, 2, 3, 4]). Python provides the built-in range() function for this purpose, which is highly efficient and flexible.

This guide explains how to convert range objects to lists, use step values for arithmetic progressions, and leverage list comprehensions for complex sequences.

Method 1: Using list(range()) (Standard Approach)

The range() function returns a "range object" (a memory-efficient iterable), not a list. To get an actual list, you must pass it to the list() constructor.

Syntax:

range(start, stop, step)
  • start: Inclusive (default 0).
  • stop: Exclusive (required).
  • step: Increment (default 1).
nums = list(range(5))
print(f"Basic: {nums}")

start_stop = list(range(10, 15))
print(f"Start-Stop: {start_stop}")

evens = list(range(0, 11, 2))
print(f"Evens: {evens}")

countdown = list(range(10, 0, -1))
print(f"Countdown: {countdown}")

Output:

Basic: [0, 1, 2, 3, 4]
Start-Stop: [10, 11, 12, 13, 14]
Evens: [0, 2, 4, 6, 8, 10]
Countdown: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Method 2: Using the Unpacking Operator (*)

Introduced in Python 3.5, the unpacking operator allows you to expand an iterable directly inside a list literal. This is slightly more concise syntax for modern Python.

nums = [*range(5)]
print(f"Unpacked: {nums}")

combined = [*range(5), *range(10, 15)]
print(f"Combined: {combined}")

Output:

Unpacked: [0, 1, 2, 3, 4]
Combined: [0, 1, 2, 3, 4, 10, 11, 12, 13, 14]

Method 3: Using List Comprehensions (Custom Logic)

If you need a sequence that isn't a simple arithmetic progression (e.g., squares of numbers, or floating point steps), list comprehensions are the tool of choice.

Squares

squares = [x**2 for x in range(1, 6)]
print(f"Squares: {squares}")

Output:

Squares: [1, 4, 9, 16, 25]

Floating Point Ranges

The range() function only supports integers. To get floats (e.g., 0.0, 0.5, 1.0), use a comprehension.

floats = [x * 0.5 for x in range(5)]
print(f"Floats: {floats}")

Output:

Floats: [0.0, 0.5, 1.0, 1.5, 2.0]

Method 4: Using NumPy (High Performance)

For scientific computing or large datasets, Python lists are inefficient. Use numpy.arange() or numpy.linspace().

import numpy as np

np_range = np.arange(0, 1, 0.2)
print(f"NumPy arange: {np_range}")

np_linspace = np.linspace(0, 10, 5) 
print(f"NumPy linspace: {np_linspace}")

Output:

NumPy arange: [0.  0.2 0.4 0.6 0.8]
NumPy linspace: [ 0.   2.5  5.   7.5 10. ]

Conclusion

To create a list with a range of numbers:

  1. Use list(range(start, stop)) for standard integer sequences.
  2. Use [*range(n)] for concise unpacking in modern Python.
  3. Use List Comprehension if you need non-linear sequences (squares) or floating point steps.
  4. Use NumPy for high-performance numerical arrays.
Navigation