How to Create a List of Numbers in Python

Introduction

To create a list of numbers in Python, the most common solution is to use range() with list().

1
2
numbers = list(range(1, 11))
print(numbers)

Output:

1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

The important thing to remember is that range(start, stop) includes the start value but excludes the stop value. So, to create a list from 1 to 10, you need to use range(1, 11).

Create a List of Numbers from 0 to n

To create a list of integers from 0 to n, use:

1
2
3
n = 9
numbers = list(range(n + 1))
print(numbers)

Output:

1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Here, n + 1 is used because the stop value is not included.

Create a List of Numbers from 1 to n

To create a list from 1 to n:

1
2
3
n = 10
numbers = list(range(1, n + 1))
print(numbers)

Output:

1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

This is one of the most common ways to create a simple list of numbers in Python.

Create a List Between Two Values

To create a list of numbers between two values, define a start and an end value:

1
2
3
4
5
start = 5
end = 12

numbers = list(range(start, end + 1))
print(numbers)

Output:

1
[5, 6, 7, 8, 9, 10, 11, 12]

Again, we use end + 1 because range() excludes the stop value.

Create a List with a Step

You can also create a list of numbers with a custom step.

1
2
numbers = list(range(0, 21, 5))
print(numbers)

Output:

1
[0, 5, 10, 15, 20]

The syntax is:

1
range(start, stop, step)

For example:

1
2
numbers = list(range(2, 11, 2))
print(numbers)

Output:

1
[2, 4, 6, 8, 10]

Create a List of Even Numbers

To create a list of even numbers, use a step of 2:

1
2
even_numbers = list(range(0, 11, 2))
print(even_numbers)

Output:

1
[0, 2, 4, 6, 8, 10]

You can also use a list comprehension:

1
2
even_numbers = [i for i in range(11) if i % 2 == 0]
print(even_numbers)

Output:

1
[0, 2, 4, 6, 8, 10]

For this simple case, list(range(0, 11, 2)) is shorter and easier to read.

Create a List of Odd Numbers

To create a list of odd numbers:

1
2
odd_numbers = list(range(1, 11, 2))
print(odd_numbers)

Output:

1
[1, 3, 5, 7, 9]

You can also use a list comprehension:

1
2
odd_numbers = [i for i in range(11) if i % 2 != 0]
print(odd_numbers)

Output:

1
[1, 3, 5, 7, 9]

Create a List of Negative Numbers

The range() function also works with negative numbers.

1
2
numbers = list(range(-5, 6))
print(numbers)

Output:

1
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]

You can also create a decreasing list using a negative step:

1
2
numbers = list(range(10, 0, -1))
print(numbers)

Output:

1
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Create a List Using a List Comprehension

List comprehensions are useful when you want to transform or filter numbers.

For example, to create a list of square numbers:

1
2
squares = [x**2 for x in range(10)]
print(squares)

Output:

1
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

You can also use conditions:

1
2
numbers = [x for x in range(-10, 21) if 5 < x < 20]
print(numbers)

Output:

1
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Create a List of Float Numbers Without NumPy

The built-in range() function works with integers, not floats. However, you can create floats by scaling integers.

1
2
floats = [x / 10 for x in range(0, 11)]
print(floats)

Output:

1
[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]

This is useful when you want a simple float list without installing any external library.

Create a List of Floats with NumPy arange()

If you use NumPy, you can create numbers with decimal steps using np.arange().

1
2
3
4
import numpy as np

numbers = np.arange(2, 8, 0.5).tolist()
print(numbers)

Output:

1
[2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5]

Use tolist() if you want to convert the NumPy array into a regular Python list.

Create Evenly Spaced Float Numbers with NumPy linspace()

If you want a fixed number of evenly spaced values between two numbers, use np.linspace().

1
2
3
4
import numpy as np

numbers = np.linspace(12, 16, 8).tolist()
print(numbers)

Output:

1
[12.0, 12.571428571428571, 13.142857142857142, 13.714285714285714, 14.285714285714286, 14.857142857142858, 15.428571428571429, 16.0]

The main difference is:

1
2
np.arange(start, stop, step)
np.linspace(start, stop, number_of_values)

Use np.arange() when you know the step size. Use np.linspace() when you know how many values you want.

Create a List of Random Integers

To create a list of random integers, use the random module.

1
2
3
4
import random

numbers = [random.randint(0, 10) for _ in range(10)]
print(numbers)

Example output:

1
[3, 8, 1, 10, 0, 4, 4, 9, 2, 6]

The output will be different each time you run the code.

Create a List of Unique Random Numbers

To create a list of unique random numbers, use random.sample().

1
2
3
4
import random

numbers = random.sample(range(1, 101), 10)
print(numbers)

Example output:

1
[45, 67, 89, 23, 5, 78, 56, 12, 34, 90]

This returns unique values because random.sample() samples without replacement.

Create a List of Days in a Month

You can use the calendar module to create a list of days in a given month.

1
2
3
4
5
6
7
8
9
from calendar import monthrange

year = 2008
month = 2

days_in_month = monthrange(year, month)[1]
days = list(range(1, days_in_month + 1))

print(days)

Output:

1
[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]

This example returns 29 days because February 2008 was in a leap year.

Summary

Here are the most common ways to create lists of numbers in Python:

1
2
3
4
5
list(range(10))          # [0, 1, 2, ..., 9]
list(range(1, 11))       # [1, 2, 3, ..., 10]
list(range(0, 11, 2))    # [0, 2, 4, 6, 8, 10]
list(range(1, 11, 2))    # [1, 3, 5, 7, 9]
list(range(10, 0, -1))   # [10, 9, 8, ..., 1]

Use list(range()) for simple integer lists. Use list comprehensions when you need filtering or transformations. Use NumPy when you need float values, decimal steps, or evenly spaced numerical arrays.

Frequently Asked Questions

How do you create a list from 1 to 10 in Python?

Use:

1
numbers = list(range(1, 11))

Output:

1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Why does range(1, 10) not include 10?

Because the stop value in range(start, stop) is excluded. To include 10, use:

1
range(1, 11)

How do you create a list from 0 to 100 in Python?

Use:

1
numbers = list(range(101))

How do you create a list of even numbers in Python?

Use:

1
even_numbers = list(range(0, 101, 2))

How do you create a list of float numbers in Python?

Without NumPy:

1
floats = [x / 10 for x in range(0, 11)]

With NumPy:

1
2
3
import numpy as np

floats = np.linspace(0, 1, 11).tolist()

References

Links Site
Python range() Python documentation
Python list() Python documentation
Python list comprehensions Python documentation
Python random.randint() Python documentation
Python random.sample() Python documentation
Python calendar.monthrange() Python documentation
NumPy arange() NumPy documentation
NumPy linspace() NumPy documentation
NumPy ndarray.tolist() NumPy documentation