You know how to store a single value. Now you need to store thousands of them — and do math on all of them at once. This lesson introduces Python lists and NumPy arrays, and by the end you'll know exactly when to use which one.
What you'll learn
What
A Python list is a flexible, ordered collection that can hold any mix of types. A NumPy array is a fixed-type, grid-like container optimized for numerical computation.
Why
Most data in the real world is not a single number — it's rows and columns. NumPy arrays are 10–100× faster than lists for numerical operations because they skip Python's overhead and call compiled C code directly.
Where it's used
NumPy arrays underpin pandas, scikit-learn, TensorFlow, and virtually every data library in the Python ecosystem.
Where this runs in production
Autopilot processes millions of lidar/camera readings per second using NumPy arrays — a Python list would be 50× too slow.
Vaccine efficacy was computed across thousands of patient records stored as NumPy arrays, enabling matrix operations in milliseconds.
The user–item rating matrix is a 2D NumPy array. Dot products across it power every 'Top Picks for You' row.
A Python list is like a shopping bag. You can put in apples (int), a book (str), and keys (bool) — anything goes, in any order. You can add more items, take items out, and the bag stretches to fit.
my_list = [10, 3.14, "hello", True] print(my_list[0]) # 10 print(my_list[-1]) # True (negative = from end) my_list.append(42) print(len(my_list)) # 5
Lists are zero-indexed — the first item is at index 0. Negative indices count from the end. .append() adds to the end without creating a new list.
A NumPy array is like a single column in Excel where every cell must be the same type. You give up flexibility — no mixing strings and numbers — but you gain the ability to say 'multiply everything by 2' and have it happen instantly, because NumPy sends the whole array to a compiled C function that runs at native CPU speed.
When you write `arr * 2` on a NumPy array, there is no Python loop. NumPy calls a C function that operates on the entire array in one shot — this is called vectorization. It is the foundation of fast numerical computing in Python.
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr * 2) # [2 4 6 8 10] print(arr.dtype) # int64 print(arr.shape) # (5,) print(arr.mean()) # 3.0
Notice: no brackets in the output (NumPy shows arrays without them), .dtype tells you the stored type, .shape is a tuple of dimensions, and .mean() is a built-in method — no import needed.
If you create np.array([1, 2, 'three']), NumPy upcasts all elements to the broadest type — in this case str (dtype '<U21'). All numbers become strings. Always pass homogeneous data to NumPy.
Click a node to see the full detail. The green nodes are NumPy advantages; the neutral nodes are list advantages.
Select a type to see its full definition, operations, and data science usage.
Store five temperature readings and print them.
We import NumPy and give it the conventional alias 'np'. This is universal — every data science codebase does this.
Your task
A weather station recorded daily high temperatures (°C) for a week. Complete the tasks below using NumPy — no Python loops allowed.
Average: 29.5°C Hottest: 33.1°C Hot days: [31.2 33.1 30.6] In Fahrenheit: [83.3 88.2 78.4 91.6 85.5 81.3 87.1]
Write your solution in the editor on the right, then hit Run.
What does `np.array([1, 2, 3]).dtype` return?
Which of these creates a 2D NumPy array with shape (2, 3)?
You have `arr = np.array([10, 20, 30, 40, 50])`. What does `arr[1:4]` return?
What is the fastest fix?
Create a NumPy array of integers from 1 to 10. Compute the sum of squares of all even numbers in the array. (Hint: use boolean masking to extract evens, then square and sum.)
You run `prices = [10, 20, 30]` then `prices * 2`. What do you get?
What is the core difference between a Python list and a NumPy array, and when would you choose each?
A Python list is heterogeneous and dynamic — it holds any mix of types and resizes freely, which makes it ideal for general-purpose collections and small, mixed records. A NumPy array is homogeneous and stored in a contiguous block of one C-level dtype, which makes it compact and fast for numerical work. I reach for a list when the data is small, mixed-type, or I'm just collecting items; I reach for a NumPy array the moment I need element-wise math, statistics, or multi-dimensional numeric data. In a data pipeline you often start with lists from parsing and convert to arrays (or a pandas column, which is array-backed) once the data is clean and numeric.
Why is `arr * 2` on a NumPy array so much faster than `[x * 2 for x in lst]` on a list?
The list comprehension runs in the Python interpreter: for each element it does a bytecode loop iteration, a dynamic type check, and a boxed-object multiply, all of which carry per-element overhead. NumPy's arr * 2 is vectorized — the loop happens inside a single compiled C routine over a contiguous, single-dtype memory block, so there's no per-element Python overhead (the hardware can even process several elements per instruction — a bonus fact, not something interviewers expect at this level). On a million elements that's the difference between seconds and milliseconds. The trade-off is that the array must be homogeneous and the operation must be one NumPy supports, which is nearly always true for numeric data.
Explain broadcasting in NumPy. Give an example.
Broadcasting is NumPy's rule for performing arithmetic on arrays of different shapes without copying data. When shapes are compatible (trailing dimensions match or are 1), NumPy stretches the smaller array conceptually. Example: np.array([[1,2,3],[4,5,6]]) + np.array([10,20,30]) — the 1D array is broadcast across both rows, giving [[11,22,33],[14,25,36]]. A scalar is the simplest case: arr * 2 broadcasts the single value 2 across every element, which is why element-wise scaling needs no loop.
Common Mistakes to Avoid
1) Expecting list * 2 to double the numbers — on a list it repeats the elements ([1,2]*2 is [1,2,1,2]); use a NumPy array for element-wise math. 2) Forgetting import numpy as np — np.array(...) raises a NameError without it. 3) Mixing types into an array: np.array([1, 2, 'x']) silently upcasts everything to strings, breaking later arithmetic. 4) Confusing .shape and len() on 2D arrays — len() gives only the number of rows, while .shape gives (rows, cols). 5) Assuming a slice is a copy: NumPy slices are VIEWS into the original array, so editing the slice edits the source; use .copy() when you need independence.
Ask the AI Tutor
Try these prompts in the AI Tutor panel: • 'ELI5: what does vectorization actually mean?' • 'Show me the same calculation done with a list loop and with a NumPy array, side by side.' • 'Quiz me on what .shape returns for a few different arrays.' • 'Give me a realistic dataset column and let me practise boolean masking on it.' • 'Interview mode: ask me why NumPy is faster than a list and grade my answer.'
Glossary
List — a built-in, ordered, resizable Python collection that can hold mixed types. NumPy — the numerical computing library imported as np. Array (ndarray) — NumPy's fixed-dtype, grid-like container for numbers. dtype — the single data type every element of an array shares (e.g. int64, float64). shape — a tuple giving an array's dimensions, like (rows, cols). Vectorization — applying an operation to a whole array in compiled C with no Python loop. Element-wise — an operation applied independently to each element (arr * 2). Boolean masking — indexing an array with a True/False array to filter elements. Broadcasting — stretching a smaller array/scalar across a larger one during arithmetic. View — a slice that shares memory with the original array rather than copying it.
Recommended Resources
• Docs: the official 'NumPy: the absolute basics for beginners' guide covers arrays, dtypes, and indexing from the source. • Read: the NumPy 'Broadcasting' page for the full rules with diagrams. • Practice: take any list of numbers, convert it to an array, and compute .mean(), .max(), and a boolean-masked subset in a REPL. • Next in DSM: you've met arrays, the backbone of every data library — the deeper NumPy toolkit arrives in the later Python for Data Science module, once the Foundations are solid.
Recap
✓ Python lists are flexible, mixed-type, resizable collections — great for general use, slow for bulk math. ✓ On a list, * repeats elements; it does not do element-wise arithmetic. ✓ NumPy arrays are homogeneous, single-dtype containers built for numerical speed. ✓ Vectorized operations like arr * 2 run in compiled C with no Python loop — often 10–100× faster. ✓ Boolean masking (arr[arr > 30]) filters an array without a loop. ✓ pandas and every major data library are built on NumPy arrays. Next up: you've completed Python Foundations. From here the curriculum builds on these primitives — variables, text, operators, conversion, and arrays — into the full data science workflow.
Run your code to see the output here.