HOWTO · Python
How to Create a List With a Specific Size in Python
Learn how to create a Python list with a specific size and choose suitable values for its elements.
On this page
Use [value] * size to create a list with a chosen number of elements when every position should start with the same value. For example, [None] * 4 creates a four-element list. Python lists hold references to objects, so this initializes four entries; it does not reserve four uninitialized slots. Choose the initializer according to what each position should contain.
Create a Fixed-Size List
Here is a complete example that creates a list with four entries initialized to None:
size = 4
items = [None] * size
print(len(items), items)
4 [None, None, None, None]
The list has length size, and each entry refers to the singleton None. Replace an entry by assigning to its index, such as items[0] = "ready". If the list must keep its length, assign to existing indexes instead of appending or removing elements; Python lists themselves remain resizable.
Repetition is concise when the initial value can safely be shared. Be careful with mutable values: [[]] * 3 puts three references to the same inner list in the outer list. A change through one position is therefore visible through the others. To create a distinct list at every position, use a comprehension such as [[] for _ in range(3)]. A comprehension is also useful when each initial value depends on its index, for example [index * 2 for index in range(size)].
A loop with append is another option when constructing each element requires several steps: start with items = [], then append one value per iteration. Use repetition for a shared immutable initializer such as None, a comprehension for independently constructed or calculated values, and a loop when it makes multi-step construction clearer. These patterns have different work and allocation behavior, so old timing figures from a particular machine should not be treated as a universal speed ranking. If speed matters, compare the alternatives with representative inputs in the Python environment used by your program. This timeit script repeats each construction 100 times, reports the fastest of three runs, and prints seconds per construction; its results describe only that run and environment:
import timeit
size = 100
number = 100
cases = {
"repetition": "[None] * size",
"comprehension": "[None for _ in range(size)]",
}
for name, statement in cases.items():
runs = timeit.repeat(
statement,
globals={"size": size},
number=number,
repeat=3,
)
print(f"{name}: {min(runs) / number:.9f} seconds per construction")
To compare a different task, change the values, size, and construction expressions to match the work your program performs. Do not treat one machine’s timings as a general ranking.
Use array.array for Typed Numeric Values
A regular list can hold values of different types and stores references to Python objects. The standard-library array.array is a typed sequence intended for compact numeric values of one supported type. For example, array('i', [0] * 4) creates four integer entries; import array from the array module first. Its type code constrains which values can be stored, so it is not a drop-in replacement when a list needs mixed objects or general Python values. Choose it when a typed numeric sequence suits the data and its operations.
Use NumPy for Large Numeric Arrays
For numerical work that benefits from array operations across many values, NumPy provides the ndarray type. numpy.zeros(4) creates an array initialized to zero, while numpy.empty(4) allocates an array without initializing its elements. The values returned by empty are unspecified; assign every element before reading it. It does not mean “create a zero-filled array.” NumPy is useful when its numeric data model and array operations fit the task, but it adds a dependency and is not necessary just to make a short Python list.
In short, use [None] * size when all list positions should initially refer to None, a comprehension when positions need separate or calculated values, array.array for a suitable single-type numeric sequence, and NumPy when numerical array operations are useful. The right choice follows the values and operations your program needs, rather than a fixed size threshold or a benchmark result from another environment.