Python
List Initialization
Duplicate Question
Python Lists
Programming Tips

Initialise a list to a specific length in Python

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Python lists do not have a separate capacity-setting step, so "initialising a list to a specific length" means creating a list that already contains that many elements. The correct method depends on what fills the list. For immutable values, multiplication is fine. For mutable values, multiplication creates shared references and usually causes bugs.

Immutable Fill Values

For values such as integers, strings, or None, list multiplication is the simplest answer.

python
1items = [0] * 5
2slots = [None] * 4
3
4print(items)
5print(slots)

This is useful when:

  • the final size is known
  • every element starts with the same immutable value
  • you plan to overwrite positions later

Using None as a placeholder is especially common:

python
results = [None] * 3
results[0] = "ready"
print(results)

The Shared-Reference Problem

The dangerous case is a mutable default value.

python
rows = [[]] * 3
rows[0].append("x")
print(rows)

This prints three lists containing "x", because all positions point to the same inner list.

That happens because multiplication repeats references, not deep copies.

The Safe Way for Mutable Elements

Use a comprehension when each slot needs its own object.

python
rows = [[] for _ in range(3)]
rows[0].append("x")
print(rows)

Now only the first row changes.

The same rule applies to dictionaries:

python
records = [{} for _ in range(2)]
records[0]["id"] = 1
print(records)

Each dictionary is independent.

Fixed-Length Lists You Fill Later

Sometimes the length matters more than the initial content. In that case, placeholders are fine.

python
1values = [None] * 5
2
3for i in range(len(values)):
4    values[i] = i * 10
5
6print(values)

This works well when position has meaning and later code naturally assigns by index.

Two-Dimensional Lists

The most common real-world trap is matrix creation. Do not write:

python
grid = [[0] * 4] * 3

Use:

python
grid = [[0] * 4 for _ in range(3)]
grid[0][1] = 99
print(grid)

This creates separate rows. The repeated-reference version does not.

When append Is the Better Choice

If the final size is not known yet, pre-filling a list is often less clear than simply appending values as they arrive.

python
1items = []
2for i in range(5):
3    items.append(i * 2)
4print(items)

This is a better fit for streaming data, filters, or loops where the number of results depends on conditions. In other words, fixed-length initialization is a structural tool, not a requirement.

Performance and Readability

Python lists already resize dynamically, so fixed-length initialisation is rarely about low-level performance. It is usually about expressing structure clearly. If the code naturally thinks in slots, placeholders make sense. If the code naturally grows a result, append is simpler.

That is why the most useful rule is semantic, not micro-optimizing:

  • use multiplication for immutable repeated values
  • use comprehensions for fresh mutable elements
  • use append when the size is not naturally known ahead of time

Common Pitfalls

  • Using [[]] * n and expecting separate inner lists.
  • Treating Python list initialisation like manual capacity reservation in C or Java.
  • Forgetting that multiplication is safe for immutable values but risky for mutable ones.
  • Building grids with shared rows by accident.
  • Using placeholders when append would better match the data flow.

Summary

  • Use [value] * n for repeated immutable values.
  • Use [None] * n for fixed-size placeholder lists.
  • Use a comprehension for mutable elements such as lists and dictionaries.
  • Be especially careful with nested lists, because shared references are a common bug.
  • In Python, fixed-length initialisation is mostly a semantic choice, not a required performance trick.

Course illustration
Course illustration

All Rights Reserved.