We want to know which is faster: a NumPy array or a Python list. This helps us choose the best tool for working with numbers.
0
0
NumPy array vs Python list performance
Introduction
When you need to do math on many numbers quickly.
When you want to store a large collection of numbers efficiently.
When you want to compare how fast different data structures work.
When you want to learn about performance in data science tasks.
Syntax
NumPy
import numpy as np import time # Create a Python list python_list = [i for i in range(1000000)] # Create a NumPy array numpy_array = np.array(python_list) # Measure time for sum using Python list def sum_python_list(numbers): total = 0 for number in numbers: total += number return total # Measure time for sum using NumPy array def sum_numpy_array(numbers): return np.sum(numbers)
NumPy arrays are made for fast math operations on numbers.
Python lists can hold any type but are slower for math on many numbers.
Examples
Both can be empty. Summing empty collections returns 0.
NumPy
python_list = [] # empty list numpy_array = np.array([]) # empty NumPy array
Both can hold one number. Summing returns that number.
NumPy
python_list = [42] # one element list numpy_array = np.array([42]) # one element NumPy array
Both hold multiple numbers. NumPy sum is usually faster.
NumPy
python_list = [1, 2, 3, 4, 5] # list with multiple elements numpy_array = np.array([1, 2, 3, 4, 5]) # NumPy array with same elements
Sample Program
This program creates a big list and a NumPy array with the same numbers. It sums them both and shows how long each takes. You will see NumPy is faster.
NumPy
import numpy as np import time # Create a Python list with 1 million numbers python_list = [i for i in range(1000000)] # Create a NumPy array from the same numbers numpy_array = np.array(python_list) # Function to sum numbers in Python list def sum_python_list(numbers): total = 0 for number in numbers: total += number return total # Function to sum numbers in NumPy array def sum_numpy_array(numbers): return np.sum(numbers) # Measure time for Python list sum start_time_list = time.time() sum_list = sum_python_list(python_list) end_time_list = time.time() # Measure time for NumPy array sum start_time_numpy = time.time() sum_numpy = sum_numpy_array(numpy_array) end_time_numpy = time.time() # Print results print(f"Sum using Python list: {sum_list}") print(f"Time taken by Python list sum: {end_time_list - start_time_list:.6f} seconds") print(f"Sum using NumPy array: {sum_numpy}") print(f"Time taken by NumPy array sum: {end_time_numpy - start_time_numpy:.6f} seconds")
OutputSuccess
Important Notes
NumPy arrays use less memory and run math operations faster than Python lists.
Python lists are flexible but slower for large number crunching.
Use NumPy arrays when working with big numeric data for better speed.
Summary
NumPy arrays are faster than Python lists for math on many numbers.
Python lists are good for mixed data but slower for numeric tasks.
Choose NumPy arrays to improve performance in data science projects.