My Notes
Python Lists vs Arrays — The Real Story
Python Lists vs Arrays — The Real Story
Wait, does Python even have arrays?
Kind of. Sort of. Not really — but also yes.
Here's the thing nobody tells you upfront: Python doesn't have a built-in array the way languages like C or Java do. When people say "array" in Python, they usually mean one of three different things depending on context. And most of the time, they actually just mean a list.
Let's break all three down.
Option 1: list — the one you'll use 90% of the time
Built into Python. No imports. No setup. Just works.
A list is ordered, changeable, and accepts any mix of types. It's Python's Swiss Army knife for storing collections of things.
skills = ["python", "security", "hacking", "ctf"]
mixed = [1, "hello", True, 3.14]
empty = []
What you can do with it
skills = ["python", "security", "hacking", "ctf"]
print(skills[0]) # python
print(skills[-1]) # ctf
print(len(skills)) # 4
skills.append("networking")
print(skills)
Output:
python
ctf
4
['python', 'security', 'hacking', 'ctf', 'networking']
Modifying, removing, slicing
skills = ["python", "security", "hacking", "ctf"]
skills[1] = "malware" # change by index
skills.remove("hacking") # remove by value
skills.insert(1, "reversing") # insert at position
print(skills)
Output:
['python', 'reversing', 'malware', 'ctf']
Slicing — grabbing a chunk of the list:
skills = ["python", "security", "hacking", "ctf", "networking"]
print(skills[1:3]) # index 1 up to (not including) 3
print(skills[:2]) # first two items
print(skills[2:]) # everything from index 2 onwards
print(skills[::-1]) # reversed
Output:
['security', 'hacking']
['python', 'security']
['hacking', 'ctf', 'networking']
['networking', 'ctf', 'hacking', 'security', 'python']
When to use a list
Any time you have a collection of things. Names, scores, URLs, mixed data, items you'll add to or remove from. If you're not sure which to pick — use a list. It's the default.
Option 2: array — the built-in one nobody really uses
Python has a built-in array module. It's like a list but locked to one data type. You have to tell it upfront what type it'll hold using a type code.
import array
nums = array.array('i', [1, 2, 3, 4, 5]) # 'i' = signed integer
print(nums[0])
print(type(nums))
Output:
1
<class 'array.array'>
Common type codes:
| Code | Type | Example |
|---|---|---|
'i' | signed int | 1, -5, 100 |
'f' | float | 3.14, 9.99 |
'd' | double (precise float) | 3.14159265 |
'b' | signed byte | 0, 127, -128 |
If you try to add the wrong type:
import array
nums = array.array('i', [1, 2, 3])
nums.append(3.14) # TypeError — floats not allowed in an int array
Output:
TypeError: integer argument expected, got float
When to use array
When you're storing millions of numbers and memory matters. A Python array uses less RAM than a list for large numerical data because every element is the same type and takes the same space. But honestly? Most people skip this entirely and jump straight to numpy when they need that kind of efficiency.
Option 3: numpy array — the one everyone actually means
This is the real deal. When a data scientist, ML engineer, or anyone doing math-heavy work says "array" in Python — they mean a numpy array.
NumPy isn't built in. You install it once:
pip install numpy
Then import it (everyone aliases it as np):
import numpy as np
nums = np.array([10, 20, 30, 40, 50])
print(nums)
print(type(nums))
Output:
[10 20 30 40 50]
<class 'numpy.ndarray'>
The superpower — math on the whole array at once
With a regular list, you can't do math directly on it:
scores = [10, 20, 30, 40]
print(scores * 2) # just duplicates the list, not what you want
Output:
[10, 20, 30, 40, 10, 20, 30, 40]
With numpy, math works the way you'd expect:
import numpy as np
scores = np.array([10, 20, 30, 40])
print(scores * 2) # multiply every element
print(scores + 5) # add 5 to every element
print(scores.mean()) # average
print(scores.sum()) # total
print(scores.max()) # highest
Output:
[20 40 60 80]
[15 25 35 45]
25.0
100
40
You didn't write a single loop. NumPy handled every element in one shot — and it does it orders of magnitude faster than a list for large data.
Multi-dimensional arrays — grids and matrices
NumPy arrays can be 2D, 3D, or more. This is how images, tables, and neural network weights are stored.
import numpy as np
grid = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
print(grid)
print(grid[0]) # first row
print(grid[1][2]) # row 1, column 2
print(grid.shape) # dimensions
Output:
[[1 2 3]
[4 5 6]
[7 8 9]]
[1 2 3]
6
(3, 3)
When to use numpy
When you're doing math on large collections of numbers. Machine learning, data analysis, image processing, scientific computing, statistics. If you're building anything in that space, numpy is non-negotiable.
Side by side — all three
# list — built-in, mixed types, flexible
skills = ["python", 42, True]
skills.append("security")
# array — built-in module, one type, memory efficient
import array
nums = array.array('i', [1, 2, 3, 4])
# numpy array — third-party, one type, math superpower
import numpy as np
scores = np.array([85, 90, 78, 95])
print(scores.mean()) # 87.0
The decision table
list | array | numpy array | |
|---|---|---|---|
| Built-in? | Yes | Yes | No (pip install) |
| Mixed types? | Yes | No | No |
| Fast math? | No | No | Yes |
| Flexible size? | Yes | Yes | Yes |
| Multi-dimensional? | Nested lists only | No | Yes |
| Use case | General everything | Rarely | Math, data, ML |
The honest answer to "which do I use?"
You're a beginner — use a list for everything right now. That's not a cop-out, that's genuinely the right answer. Lists cover almost every real-world use case you'll hit in the next few months.
When you start doing data science or machine learning work, that's when you add numpy to your toolkit. The array module lives in an awkward middle ground that most Python developers never touch.
If you can only remember one rule: when in doubt, it's a list.