How to Create a List of Numbers in Python ?

Introduction

Python offers powerful tools for creating lists of integers, floats, or random numbers. Below, we explore different methods for generating these lists efficiently, using built-in Python capabilities and external libraries like NumPy.

Creating Lists of Integers

A Range of Integers from 0 to ( n )

To create a list of integers from 0 to ( n ), use the range() function.

Using list comprehension:

1
2
n = 9
[i for i in range(n+1)]

Output:

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

Using the list() function:

1
list(range(n+1))

Output:

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

Even Numbers

Generate a list of even numbers by filtering integers divisible by 2:

1
[i for i in range(10) if i % 2 == 0]

Output:

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

Odd Numbers

For odd numbers, filter integers that are not divisible by 2:

1
[i for i in range(10) if i % 2 != 0]

Output:

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

A Range of Integers from ( m ) to ( n )

Define custom start (( m )) and end (( n )) points:

1
2
m, n = 1, 9
[i for i in range(m, n+1)]

Output:

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

For negative ranges:

1
[i for i in range(-10, 11)]

Output:

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

Conditional Integer Lists

Apply multiple conditions to filter your list:

1
[x for x in range(-10, 21) if x > 5 and x < 20]

Output:

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

Using Functions to Populate Lists

Define functions to apply transformations, such as squaring numbers:

1
2
3
4
def square(x):
    return x**2

[square(x) for x in range(10)]

Output:

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

Creating Lists of Floats

Using arange from NumPy

To create floats with a specific interval:

1
2
3
import numpy as np
i_start, i_end, step = 2, 8, 0.5
list(np.arange(i_start, i_end, step))

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]

Using linspace from NumPy

For evenly spaced floats within a range:

1
2
i_start, i_end, count = 12, 16, 8
list(np.linspace(i_start, i_end, count))

Output:

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

Creating Lists of Random Numbers

Random Integers

Use randint from the random module:

1
2
import random
[random.randint(0, 1) for _ in range(10)]

Output:

1
[1, 0, 1, 1, 0, 0, 1, 1, 0, 0]  # Varies with each execution

Random Samples

Generate a sample of unique random integers:

1
random.sample(range(1, 100), 10)

Output:

1
[45, 67, 89, 23, 5, 78, 56, 12, 34, 90]  # Varies with each execution

Specialized Lists

Days of a Specific Month

Generate a list of integers representing the days in a month using calendar's monthrange function:

1
2
3
4
5
from calendar import monthrange

year, month = 2008, 2
days_in_month = monthrange(year, month)[1]
[day for day in range(1, days_in_month + 1)]

Output:

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

References

Liens Site
List Comprehensions python doc
list() python doc
numpy.linspace scipy doc
numpy.arange scipy doc