A Python vector is usually represented as a one-dimensional sequence of numbers. In practical Python code, the best tool for vector math is a NumPy array because it supports fast element-wise operations, dot products, norms, and shape-aware calculations.
Plain Python lists can store vector-like data, but adding two lists concatenates them instead of adding values element by element. NumPy arrays give mathematical behavior, which is why they are the standard choice for data science, linear algebra, simulations, and numerical code.
Use a list when you only need to collect values, append items, or pass simple data around. Convert to a NumPy vector when you need arithmetic, distances, normalization, or operations across many values. Keeping that boundary clear avoids confusing list behavior with numeric array behavior.
Create a Vector With np.array()
Use np.array() to create a one-dimensional vector. Set a numeric dtype when the values should be treated consistently.
import numpy as np
vector = np.array([2, 4, 6], dtype=float)
print(vector)
print(vector.shape)
The shape (3,) means this is a one-dimensional array with three values. If you need a refresher on dimensions and nested structures, see list shape in Python. Checking shape early is one of the easiest ways to prevent incorrect vector math.
Add and Scale Vectors
NumPy applies arithmetic element by element when arrays have compatible shapes. That makes vector addition and scalar multiplication concise.
import numpy as np
a = np.array([2, 4, 6])
b = np.array([3, 5, 7])
print(a + b)
print(a * 10)
This behavior is different from Python lists. With lists, [2, 4] + [3, 5] creates a longer list. With NumPy arrays, the same idea adds matching elements. If the shapes do not match, NumPy may raise an error or apply broadcasting, so inspect the result before trusting it.
Check Shapes Before Broadcasting
Broadcasting lets NumPy combine arrays with different but compatible shapes. It is powerful, but it can also hide mistakes if you expected two vectors to have exactly the same length.
For simple vector math, start by checking a.shape == b.shape. If shapes differ intentionally, leave a short comment or variable name that explains the broadcast. That makes later maintenance safer, especially in data pipelines where column counts can change.
Calculate a Dot Product
The dot product multiplies matching elements and sums the results. It is common in projections, similarity scores, and linear algebra formulas.
import numpy as np
a = np.array([2, 4, 6])
b = np.array([3, 5, 7])
dot_product = np.dot(a, b)
print(dot_product)
For one-dimensional arrays, np.dot(a, b) returns a scalar. For higher-dimensional arrays, dot-product behavior depends on axes, so check shapes before using it in matrix code. If you only need element-wise multiplication, use a * b instead.
Find the Length of a Vector
The Euclidean length of a vector is called its L2 norm. NumPy provides this through np.linalg.norm().
import numpy as np
vector = np.array([3, 4])
length = np.linalg.norm(vector)
print(length)
This prints 5.0 because the vector length is the square root of 3**2 + 4**2. Norms are useful for distances, normalization, and measuring vector magnitude. A zero vector has length zero, so handle that case before dividing by the norm.
Stack Vectors Into a Matrix
When several vectors have the same length, stack them into a two-dimensional array. This is useful when each vector is a row of observations or features.
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
z = np.array([7, 8, 9])
matrix = np.stack([x, y, z])
print(matrix)
print(matrix.shape)
Stacking creates a new array. If you are trying to understand whether operations share memory or copy data, the guide to NumPy views is a useful next read.
Convert Between Lists and Vectors
Many APIs return plain lists. Convert to a NumPy array before vector math, then convert back with tolist() only when a list is required by another API.
import numpy as np
values = [10, 20, 30]
vector = np.array(values)
centered = vector - vector.mean()
print(centered)
print(centered.tolist())
For quick summaries of vector values, see Python average of list. For finding the position of an extreme value, see Python list max index.
Common Mistakes
The most common mistake is expecting Python lists to behave like mathematical vectors. Another is mixing arrays with different shapes and relying on broadcasting without checking the result. A third is using dot products when element-wise multiplication was intended.
Use NumPy arrays for vector math, inspect .shape when results look surprising, and keep list conversion at the boundaries of your program. For most numeric workflows, a Python vector should be a NumPy array, not a manually managed list of numbers. Lists are still fine for small collections and nonnumeric data, but convert before doing real numeric operations.