Initialising an array of fixed size in Python
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Python does not have a built-in fixed-size array type like C or Java. Instead, Python provides lists (dynamic arrays), the array module (typed arrays), and NumPy arrays (fixed-size, high-performance). Each approach has different trade-offs for memory efficiency, type safety, and performance.
Method 1: List Multiplication
The simplest way to create a list of a fixed size filled with a default value:
Warning: This creates references for mutable objects, not independent copies:
Method 2: List Comprehension
More flexible initialization with computed values:
Method 3: array Module (Typed)
Python's array module provides compact typed arrays:
Type codes:
| Code | Type | Size (bytes) |
'b' | signed char | 1 |
'i' | signed int | 2-4 |
'l' | signed long | 4-8 |
'f' | float | 4 |
'd' | double | 8 |
Method 4: NumPy Arrays (Best for Numeric Data)
NumPy provides true fixed-size, contiguous-memory arrays:
Method 5: bytearray (For Bytes)
Performance Comparison
Lists have significant per-element overhead (~56 bytes per int object + 8 bytes per pointer) compared to NumPy and array (8 bytes per int64).
Common Pitfalls
- Mutable default trap:
[[]] * ncreates n references to the same inner list. Always use[[] for _ in range(n)]for mutable elements. - Lists are not fixed-size: Python lists can grow with
append(). If you need strict fixed-size enforcement, use NumPy arrays or wrap a list in a class that prevents resizing. - NumPy empty vs zeros:
np.empty()is faster but contains uninitialized (garbage) data. Always usenp.zeros()unless you will immediately overwrite every element. - Type enforcement: Lists accept any type;
arrayand NumPy enforce types. Inserting a string into anarray('i', ...)raisesTypeError. - Memory layout: NumPy arrays store data contiguously in memory, enabling SIMD and cache-friendly operations. Lists store pointers to scattered objects.
Summary
- Use
[value] * nfor quick list initialization with immutable defaults - Use list comprehensions for mutable elements:
[[] for _ in range(n)] - Use
numpy.zeros(n)ornumpy.full(n, value)for numeric fixed-size arrays - Use
array.arrayfor typed, compact arrays without the NumPy dependency - Lists are dynamic — Python has no built-in mechanism to prevent resizing
Related reading
- Initialize a byte array to a certain value, other than the default null?
- Initializing a list to a known number of elements in Python
- Initializing Half-edge data structure from vertices
- Initializing tensorflow Variable with an array larger than 2GB
- Initializing metatrader in python
- Input image dtype is bool. Interpolation is not defined with bool data type
- Inline instantiation of a constant List
- Inline list initialization in VB.NET

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.