0
0
NumpyHow-ToBeginner ยท 3 min read

Why NumPy is Faster Than Python List: Key Reasons Explained

NumPy is faster than Python list because it stores data in a compact, fixed-type array allowing efficient memory use and vectorized operations. Unlike lists, which hold references to objects, NumPy arrays use contiguous memory and optimized C code for computations.
๐Ÿ“

Syntax

NumPy arrays are created using numpy.array(). This function takes a list or other sequence and converts it into a fixed-type, contiguous memory array. This structure allows fast numerical operations.

  • numpy.array(data): Converts data (like a list) into a NumPy array.
  • Arrays have a fixed data type, unlike lists which can hold mixed types.
  • Operations on arrays apply to all elements at once (vectorized), speeding up calculations.
python
import numpy as np

arr = np.array([1, 2, 3, 4])
print(arr)
print(type(arr))
Output
[1 2 3 4] <class 'numpy.ndarray'>
๐Ÿ’ป

Example

This example compares the speed of adding two lists element-wise versus adding two NumPy arrays. The NumPy version is much faster because it uses optimized C code and vectorized operations.

python
import numpy as np
import time

# Create large lists and arrays
list1 = list(range(1000000))
list2 = list(range(1000000))
arr1 = np.array(list1)
arr2 = np.array(list2)

# Add lists element-wise using a loop
start = time.time()
result_list = [x + y for x, y in zip(list1, list2)]
end = time.time()
list_time = end - start

# Add arrays element-wise using NumPy
start = time.time()
result_arr = arr1 + arr2
end = time.time()
arr_time = end - start

print(f"List addition time: {list_time:.5f} seconds")
print(f"NumPy addition time: {arr_time:.5f} seconds")
Output
List addition time: 0.15000 seconds NumPy addition time: 0.00500 seconds
โš ๏ธ

Common Pitfalls

One common mistake is expecting NumPy arrays to behave exactly like Python lists. For example, NumPy arrays have fixed data types, so mixing types can cause unexpected results or errors.

Another pitfall is using Python loops on NumPy arrays instead of vectorized operations, which loses the speed advantage.

python
import numpy as np

# Wrong: mixing types in NumPy array
arr = np.array([1, 'two', 3])
print(arr)
print(arr.dtype)

# Wrong: slow loop on NumPy array
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = []
for i in range(len(arr1)):
    result.append(arr1[i] + arr2[i])
print(result)

# Right: vectorized addition
result_vec = arr1 + arr2
print(result_vec)
Output
['1' 'two' '3'] <U21 [5, 7, 9] [5 7 9]
๐Ÿ“Š

Quick Reference

Summary tips to understand why NumPy is faster than lists:

  • Memory: NumPy arrays use contiguous memory blocks; lists store pointers.
  • Data type: Arrays have fixed types, enabling optimized code.
  • Operations: Vectorized operations run in compiled code, not Python loops.
  • Use cases: NumPy excels in numerical and large data tasks.
โœ…

Key Takeaways

NumPy arrays are faster because they use fixed-type, contiguous memory storage.
Vectorized operations in NumPy run compiled code, avoiding slow Python loops.
Python lists store references to objects, causing more memory use and slower access.
Avoid mixing data types in NumPy arrays to keep performance and correctness.
Use NumPy for numerical tasks to gain significant speed improvements.