How to declare and add items to an array 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
In Python, most developers use lists when they say array, but Python also provides array.array and NumPy arrays for typed numeric workloads. Choosing the right structure depends on performance, memory, and API needs. This guide explains how to declare each structure and add items safely.
Use Lists For General Purpose Collections
Lists are dynamic, allow mixed types, and support many insertion methods.
Key operations:
append(x)adds one item to end.extend(iterable)adds many items.insert(i, x)adds at a position.
Lists are ideal for application level data where flexibility matters more than strict type control.
Typed Arrays With array.array
If you need compact storage for many numbers of one type, use the built in array module.
Type codes enforce element type, for example 'i' for signed integers or 'f' for floats. Attempting to append an incompatible type raises an error.
NumPy Arrays For Numerical Computing
For data science or numeric heavy workloads, NumPy arrays are usually the best choice.
Note that np.append returns a new array. It does not modify in place. For frequent growth operations, collect values in a Python list first, then convert once to NumPy.
Choosing The Right Structure
Use this practical rule:
- List for flexible general objects.
array.arrayfor memory efficient typed simple numbers.- NumPy for vectorized numerical operations and matrix style workflows.
Do not force typed arrays where plain lists are simpler and more readable.
Adding Multiple Items Efficiently
Repeated concatenation with + can be slower for large loops because it creates new lists each time. Prefer append inside loops and extend for batches.
This avoids unnecessary temporary objects.
Nested Arrays And Initialization
For nested structures, avoid multiplying mutable lists directly because inner rows may reference the same object.
This is a frequent bug in beginner and intermediate Python code.
Conversion Between Structures
You can convert between list and typed arrays when needed.
For NumPy conversion:
Conversion is useful at boundaries between libraries.
Practical API Design Tips
If your function accepts a sequence input, prefer type hints such as Sequence or Iterable instead of forcing callers to pass a list. This keeps your API flexible and lets users provide generators when appropriate.
When your function needs mutation operations like append, convert once internally and document that conversion behavior. Clear contracts make performance expectations explicit.
For team codebases, adopt a convention for when NumPy arrays are required versus optional. This avoids repeated conversions and inconsistent interfaces across modules.
Common Pitfalls
- Calling Python lists arrays and expecting typed behavior.
- Using
np.appendin tight loops and creating many copies. - Forgetting that
array.arrayenforces one data type. - Creating nested lists with shared row references.
- Choosing complex structures when a plain list is sufficient.
Summary
- Python lists are the default dynamic array like structure.
array.arraygives typed, compact storage for simple numeric values.- NumPy arrays are best for numerical and vectorized workflows.
- Prefer
appendandextendfor efficient growth patterns. - Understand structure semantics before optimizing for performance.
Related reading
- How to declare array of zeros in python or an array of a certain size
- How to decode a property with type of JSON dictionary in Swift 45 decodable protocol
- How to decode Unicode string in Tensorflow's graph pipeline
- How to deep copy a list?
- How to decode/deserialize Avro with Python from Kafka
- How to delete a character from a string using Python
- How to define a List bean in Spring?
- How to define a two-dimensional array?

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.