NumPy Lesson 2 – Arrays Vs Lists | Dataplexa

Arrays vs Python Lists

Before diving deeper into NumPy, it is important to understand why NumPy arrays exist and how they differ from regular Python lists. At first glance, they may look similar, but internally they work very differently.


Python Lists: A Quick Review

A Python list is a general-purpose container that can store different types of values together.

numbers = [10, 20, 30, 40, 50]
print(numbers)

Output:

[10, 20, 30, 40, 50]

Python lists are flexible and easy to use, but that flexibility comes at a cost when dealing with large numerical data.


NumPy Arrays: Designed for Numbers

NumPy arrays are designed specifically for numerical computation. All elements in a NumPy array must be of the same data type.

import numpy as np

arr = np.array([10, 20, 30, 40, 50])
print(arr)

Output:

[10 20 30 40 50]

This uniform structure allows NumPy to store data more efficiently and perform operations much faster.


Key Difference 1: Data Type Consistency

A Python list can hold different data types in the same list.

mixed_list = [10, "apple", 3.5, True]
print(mixed_list)

Output:

[10, 'apple', 3.5, True]

A NumPy array automatically converts all values to a single compatible type.

arr = np.array([10, 3.5, 7])
print(arr)

Output:

[10.   3.5  7. ]

NumPy converted all values to floats to maintain consistency.


Key Difference 2: Mathematical Operations

Let’s try multiplying a Python list by a number.

numbers = [1, 2, 3]
print(numbers * 3)

Output:

[1, 2, 3, 1, 2, 3, 1, 2, 3]

This repeats the list instead of performing numeric multiplication.

Now the same operation with a NumPy array:

arr = np.array([1, 2, 3])
print(arr * 3)

Output:

[3 6 9]

NumPy performs element-wise multiplication, which is what we expect in numerical computations.


Key Difference 3: Performance

NumPy arrays are much faster than Python lists when performing operations on large datasets.

Example conceptually:

  • Python lists use loops executed in Python
  • NumPy uses optimized C loops internally

This difference becomes significant when working with thousands or millions of numbers.


Key Difference 4: Memory Usage

Python lists store references to objects, which increases memory usage. NumPy arrays store data in a compact, contiguous block of memory.

This makes NumPy more memory-efficient for numerical data.


When to Use Lists vs NumPy Arrays

Use Python lists when:

  • Working with small collections
  • Storing mixed data types
  • Flexibility is more important than speed

Use NumPy arrays when:

  • Working with numerical data
  • Performing mathematical operations
  • Handling large datasets
  • Needing high performance

Practice Exercise

Exercise

Create a Python list and a NumPy array with the same values. Multiply both by 2 and observe the difference in output.

Expected Observation

The Python list will repeat its elements, while the NumPy array will perform numeric multiplication.


What’s Next?

In the next lesson, you will learn different ways to create NumPy arrays using built-in NumPy functions.