NumPy
array initialization
fill
duplicate
Python

NumPy array initialization fill with identical values

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

NumPy provides several functions to create arrays pre-filled with a specific value. np.full() is the most direct — it creates an array of a given shape filled with a specified value. Other options include np.zeros(), np.ones(), and np.empty() followed by fill(). The choice depends on the fill value and whether you need control over dtype and memory allocation.

python
1import numpy as np
2
3# Fill with any value
4arr = np.full((3, 4), 7)
5print(arr)
6# [[7 7 7 7]
7#  [7 7 7 7]
8#  [7 7 7 7]]
9
10# Fill with a float
11arr = np.full((2, 3), 3.14)
12print(arr.dtype)  # float64
13
14# Fill with a specific dtype
15arr = np.full((2, 3), 255, dtype=np.uint8)
16print(arr.dtype)  # uint8
17
18# 1D array
19arr = np.full(5, -1)
20print(arr)  # [-1 -1 -1 -1 -1]
21
22# Fill with infinity
23arr = np.full((2, 2), np.inf)
24# Fill with NaN
25arr = np.full((2, 2), np.nan)

np.full() is the clearest way to create a filled array. It accepts any scalar value and infers or accepts a dtype.

np.zeros() and np.ones()

python
1# All zeros
2zeros = np.zeros((3, 4))
3print(zeros)
4# [[0. 0. 0. 0.]
5#  [0. 0. 0. 0.]
6#  [0. 0. 0. 0.]]
7
8# All ones
9ones = np.ones((3, 4))
10print(ones)
11# [[1. 1. 1. 1.]
12#  [1. 1. 1. 1.]
13#  [1. 1. 1. 1.]]
14
15# Integer zeros
16zeros_int = np.zeros((2, 3), dtype=int)
17print(zeros_int.dtype)  # int64
18
19# Boolean array
20flags = np.zeros(5, dtype=bool)
21print(flags)  # [False False False False False]

np.empty() + fill()

np.empty() allocates memory without initializing it (faster), then fill() sets all values:

python
1# Allocate then fill
2arr = np.empty((3, 4))
3arr.fill(42)
4print(arr)
5# [[42. 42. 42. 42.]
6#  [42. 42. 42. 42.]
7#  [42. 42. 42. 42.]]
8
9# Slightly faster than np.full for very large arrays
10arr = np.empty(10_000_000)
11arr.fill(3.14)

The speed difference is negligible for most use cases. Prefer np.full() for clarity.

Multiplication Trick

python
1# Multiply ones by the desired value
2arr = np.ones((3, 4)) * 7
3print(arr)
4# [[7. 7. 7. 7.]
5#  [7. 7. 7. 7.]
6#  [7. 7. 7. 7.]]
7
8# Works but creates an intermediate array — less efficient than np.full

_like Functions (Match Existing Array Shape)

python
1existing = np.array([[1, 2, 3], [4, 5, 6]])
2
3# Same shape, filled with zeros
4z = np.zeros_like(existing)
5print(z)  # [[0 0 0] [0 0 0]]
6
7# Same shape, filled with ones
8o = np.ones_like(existing)
9print(o)  # [[1 1 1] [1 1 1]]
10
11# Same shape, filled with custom value
12f = np.full_like(existing, 99)
13print(f)  # [[99 99 99] [99 99 99]]
14
15# Different dtype
16f = np.full_like(existing, 3.14, dtype=float)
17print(f)  # [[3.14 3.14 3.14] [3.14 3.14 3.14]]

np.repeat and np.tile

For creating arrays with repeated patterns:

python
1# Repeat a single value
2arr = np.repeat(5, 10)
3print(arr)  # [5 5 5 5 5 5 5 5 5 5]
4
5# Repeat a pattern
6arr = np.tile([1, 2, 3], 4)
7print(arr)  # [1 2 3 1 2 3 1 2 3 1 2 3]
8
9# Tile into 2D
10arr = np.tile([1, 2, 3], (3, 1))
11print(arr)
12# [[1 2 3]
13#  [1 2 3]
14#  [1 2 3]]

Performance Comparison

python
1import timeit
2
3shape = (1000, 1000)
4
5# np.full — single allocation
6timeit.timeit(lambda: np.full(shape, 42), number=1000)
7
8# np.zeros — slightly faster for zero-fill (OS may provide zeroed pages)
9timeit.timeit(lambda: np.zeros(shape), number=1000)
10
11# np.empty + fill — two steps
12timeit.timeit(lambda: np.empty(shape).__setattr__('fill', 42), number=1000)
13
14# np.ones * value — creates intermediate array
15timeit.timeit(lambda: np.ones(shape) * 42, number=1000)

For zero-filled arrays, np.zeros() is fastest because the OS may provide pre-zeroed memory pages. For other values, np.full() is optimal.

Common Pitfalls

  • Using np.empty() without filling: np.empty() does not initialize values — the array contains whatever was in memory. Reading from an unfilled np.empty() array produces garbage values, not zeros.
  • Unintended dtype from np.full(): np.full((3,3), 7) creates an int64 array, but np.full((3,3), 7.0) creates float64. The fill value determines the dtype if not specified explicitly. Always pass dtype when the type matters.
  • np.zeros returns floats by default: np.zeros((3,3)) creates float64, not int. Pass dtype=int if you need integers. This catches many people when using zeros as indices or counts.
  • Modifying shared references: arr = np.full((3,3), [1,2,3]) does not fill with a list — it broadcasts the list across rows. For object arrays with mutable elements, modifications to one element affect all (use dtype=object carefully).
  • np.ones() * value creates a temporary array: This allocates two arrays (the ones array and the result) instead of one. np.full() is both clearer and more memory-efficient.

Summary

  • Use np.full(shape, value) to fill an array with any value — the recommended approach
  • Use np.zeros(shape) for zero-filled arrays and np.ones(shape) for ones
  • Use np.full_like(arr, value) to match an existing array's shape and dtype
  • np.empty() is fastest for allocation but does not initialize values — always fill afterward
  • Always specify dtype explicitly when the type matters (int vs float)
  • Avoid np.ones() * value — it is less efficient and less readable than np.full()

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.