{"course":{"slug":"python-for-ai","title":"Python, from Zero, for AI","url":"https://addaly.com/learn/python-for-ai","level":"beginner","track":"build"},"lesson":{"slug":"numpy-arrays","title":"NumPy arrays: why one line beats a loop by a hundred times","url":"https://addaly.com/learn/python-for-ai/numpy-arrays","position":70,"of":89,"minutes":9,"answer":"A NumPy array is one block of memory holding values of a single type, so an operation on it is a compiled loop over raw numbers.","keyIdea":"A NumPy array is one block of memory holding values of a single type, so an operation on it is a compiled loop over raw numbers; a Python list is a row of pointers to separate objects, and the loop over it pays for type checks and allocation on every element."},"body":{"format":"markdown","text":"## Two ways to hold a million numbers\n\n```python\nnums = [float(i) for i in range(1_000_000)]\n```\n\nA Python list is a row of pointers. Each pointer leads to a separate float object somewhere in memory, and each float object carries a type tag, a reference count and the value: 24 bytes for an 8-byte number. Squaring them means, for each element: follow the pointer, check that the object is a float, extract the value, compute, allocate a new float object, store the pointer. A million times.\n\n```python\nimport numpy as np\narr = np.arange(1_000_000, dtype=np.float64)\n```\n\nA NumPy array is one contiguous block of raw bytes, 8 million of them here, with a single header saying \"these are float64, there are a million of them\". Squaring means a compiled C loop walking that block, no type checks, no allocation per element, and the CPU's own vector instructions doing several at a time.\n\n> Figure: A row of pointers, and a block of numbers. list of 1,000,000 floats: A row of pointers, eight bytes each; Each pointer leads to a separate float object; Each object carries a type tag and a reference count; Around 40 MB, scattered across memory; Holds anything: numbers, strings, other lists. np.array of 1,000,000 float64: One contiguous block of 8,000,000 bytes; No per-element object at all; One header: float64, one million of them; 8 MB, in one place the CPU can stream; Holds exactly one dtype, and that is the bargain. Five times the memory is the smaller half of the story. The hundredfold speed difference is the type lookup, the method dispatch and the allocation that Python performs for every single element, and that NumPy performs once for the whole array.\n\nMeasure it:\n\n```python\n%timeit [x * x for x in nums]    # ~120 ms\n%timeit arr ** 2                  # ~1.5 ms\n```\n\nThat ratio is the reason NumPy exists, and the reason every numeric library in Python (pandas, scikit-learn, PyTorch's CPU tensors) is built on it or mirrors it. It is not multithreading. It is not magic. It is memory layout plus compiled loops, and understanding that tells you when it will and will not help.\n\n## Making arrays\n\n```python\nnp.array([1, 2, 3])                   # from a list\nnp.zeros(5)                           # [0., 0., 0., 0., 0.]\nnp.ones((2, 3))                       # 2 rows, 3 columns of 1.\nnp.arange(0, 10, 2)                   # [0, 2, 4, 6, 8]\nnp.linspace(0, 1, 5)                  # [0., 0.25, 0.5, 0.75, 1.]\nnp.random.default_rng(0).random(4)    # 4 floats in [0, 1), seeded\n```\n\nThe seed matters. `default_rng(0)` gives the same \"random\" numbers every run, which is what you want when a result has to be reproducible. Unseeded, a test that passes today fails tomorrow for no reason you can find.\n\nEvery array has three attributes worth printing before you do anything with it:\n\n```python\narr.shape      # (1000000,)\narr.dtype      # float64\narr.ndim       # 1\n```\n\n## Vectorised operations\n\nArithmetic applies elementwise, and so do comparisons and most maths functions:\n\n```python\na = np.array([1., 2., 3.])\nb = np.array([10., 20., 30.])\na + b            # [11., 22., 33.]\na * 2            # [2., 4., 6.]\na > 1.5          # [False, True, True]\nnp.sqrt(a)       # elementwise square root\nnp.exp(a)        # elementwise e^x\n```\n\n`np.sqrt(a)` on an array is not the same as `math.sqrt(a)`, which expects one number and raises on an array. The NumPy versions (called **ufuncs**) accept arrays and broadcast, which the next lesson covers.\n\nReductions collapse an array to a number:\n\n```python\na.sum(), a.mean(), a.max(), a.argmax(), a.std()\n```\n\n`argmax` returns the *index* of the largest value rather than the value: the thing you want when the array is scores and you need to know which item won.\n\n## The loop you should not write\n\n```python\ntotal = 0\nfor x in arr:          # slow: pulls each element out as a Python float\n    total += x\n```\n\nIterating over a NumPy array in Python throws away the advantage; each element is boxed back into a Python object on the way out. If you find yourself writing `for i in range(len(arr))`, stop and ask what the whole-array expression is. Usually it exists: a comparison, a `where`, a reduction, a dot product. The lesson on masks shows the common ones.\n\nWhen it does not exist (the computation depends on the previous element in a way no ufunc expresses) the honest options are `numba` (a free just-in-time compiler that makes such loops fast with one decorator) or accepting the loop for a small array.\n\n## When NumPy does not help\n\n- **Small arrays.** For ten numbers, the overhead of calling into C exceeds the loop. NumPy wins from a few hundred elements up.\n- **Mixed types.** An array holds one dtype. A column of names and prices is not an array; that is pandas' job.\n- **Growing one element at a time.** `np.append` copies the whole array each call, so building an array by appending in a loop is quadratic. Collect in a Python list, then `np.array(list)` once at the end.\n- **Objects.** `np.array([\"a\", \"b\"])` works but gives you fixed-width strings or `object` dtype, and object arrays are Python lists in a NumPy coat: no speed-up.\n\n## Where this goes\n\nEvery embedding you fetch from a model is an array of floats, 384 to 3,072 of them. Every batch of them is a two-dimensional array. Similarity search is a matrix multiplication over that array, and the reason it runs over a hundred thousand vectors in milliseconds is exactly the layout described above. Two lessons from now you will write it.\n\n## Try this now\n\nBuild the million-element list and array, time squaring each with `%timeit` or `timeit`, and write the ratio down. Then time `sum(nums)` against `arr.sum()`, and a Python `for` loop over `arr` against `arr.sum()`, to see what iterating an array in Python costs."},"check":{"question":"Squaring a million numbers takes 120 ms as a Python list comprehension and 1.5 ms as `arr ** 2` on a NumPy array. A learner explains this as \"NumPy uses all the CPU cores\". What is the more accurate account?","options":[{"text":"NumPy compiles the expression to machine code at first use and caches it, so later calls skip Python entirely, which is why the second run of any expression is fast and the first is not.","correct":false,"tempting":"NumPy does not compile expressions; its speed comes from precompiled loops in C that run over contiguous memory."},{"text":"Python's interpreter caps loop speed at a fixed number of iterations per millisecond, and NumPy bypasses the interpreter.","correct":false,"tempting":"There is no such cap; the interpreter is slow per element because of the work each element requires, not a rate limit."},{"text":"The list version allocates a new list, and allocation dominates; pre-allocating the list would make it as fast as the array.","correct":false,"tempting":"Pre-allocating helps a little, but the per-element object creation and dispatch remain, and the gap stays close to two orders of magnitude."},{"text":"The list holds a separate Python object per number, each type-checked and unboxed per iteration; the array holds raw values contiguously and a compiled loop runs over them.","correct":true}]},"prev":{"slug":"profiling-where-time-goes","title":"Profiling: finding where the time actually goes before you optimise anything","url":"https://addaly.com/learn/python-for-ai/profiling-where-time-goes"},"next":{"slug":"shapes-and-broadcasting","title":"Shapes, axes and broadcasting: the wrong-shape bug that runs without complaint","url":"https://addaly.com/learn/python-for-ai/shapes-and-broadcasting"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'NumPy arrays: why one line beats a loop by a hundred times', in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/numpy-arrays (Accessed: 21 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"NumPy arrays: why one line beats a loop by a hundred times","courseTitle":"Python, from Zero, for AI","module":{"index":8,"slug":"numbers-in-bulk","title":"Numbers in bulk: NumPy, pandas and the vector under every model"},"answer":null,"keyIdea":"A NumPy array is one block of memory holding values of a single type, so an operation on it is a compiled loop over raw numbers; a Python list is a row of pointers to separate objects, and the loop over it pays for type checks and allocation on every element.","hero":{"kind":"figure","figure":{"t":"figure","label":"Figure 8.1","figure":{"caption":"Five times the memory is the smaller half of the story. The hundredfold speed difference is the type lookup, the method dispatch and the allocation that Python performs for every single element, and that NumPy performs once for the whole array.","left":{"title":"list of 1,000,000 floats","items":["A row of pointers, eight bytes each","Each pointer leads to a separate float object","Each object carries a type tag and a reference count","Around 40 MB, scattered across memory","Holds anything: numbers, strings, other lists"]},"right":{"title":"np.array of 1,000,000 float64","items":["One contiguous block of 8,000,000 bytes","No per-element object at all","One header: float64, one million of them","8 MB, in one place the CPU can stream","Holds exactly one dtype, and that is the bargain"]},"title":"A row of pointers, and a block of numbers","type":"compare"},"alt":"Figure: A row of pointers, and a block of numbers. list of 1,000,000 floats: A row of pointers, eight bytes each; Each pointer leads to a separate float object; Each object carries a type tag and a reference count; Around 40 MB, scattered across memory; Holds anything: numbers, strings, other lists. np.array of 1,000,000 float64: One contiguous block of 8,000,000 bytes; No per-element object at all; One header: float64, one million of them; 8 MB, in one place the CPU can stream; Holds exactly one dtype, and that is the bargain. Five times the memory is the smaller half of the story. The hundredfold speed difference is the type lookup, the method dispatch and the allocation that Python performs for every single element, and that NumPy performs once for the whole array."}},"blocks":[{"t":"h2","id":"two-ways-to-hold-a-million-numbers","runs":[{"text":"Two ways to hold a million numbers"}]},{"t":"code","lang":"python","label":"Python","text":"nums = [float(i) for i in range(1_000_000)]"},{"t":"p","runs":[{"text":"A Python list is a row of pointers. Each pointer leads to a separate float object somewhere in memory, and each float object carries a type tag, a reference count and the value: 24 bytes for an 8-byte number. Squaring them means, for each element: follow the pointer, check that the object is a float, extract the value, compute, allocate a new float object, store the pointer. A million times."}]},{"t":"code","lang":"python","label":"Python","text":"import numpy as np\narr = np.arange(1_000_000, dtype=np.float64)"},{"t":"p","runs":[{"text":"A NumPy array is one contiguous block of raw bytes, 8 million of them here, with a single header saying “these are float64, there are a million of them”. Squaring means a compiled C loop walking that block, no type checks, no allocation per element, and the CPU’s own vector instructions doing several at a time."}]},{"t":"p","runs":[{"text":"Measure it:"}]},{"t":"code","lang":"python","label":"Python","text":"%timeit [x * x for x in nums]    # ~120 ms\n%timeit arr ** 2                  # ~1.5 ms"},{"t":"p","runs":[{"text":"That ratio is the reason NumPy exists, and the reason every numeric library in Python (pandas, scikit-learn, PyTorch’s CPU tensors) is built on it or mirrors it. It is not multithreading. It is not magic. It is memory layout plus compiled loops, and understanding that tells you when it will and will not help."}]},{"t":"h2","id":"making-arrays","runs":[{"text":"Making arrays"}]},{"t":"code","lang":"python","label":"Python","text":"np.array([1, 2, 3])                   # from a list\nnp.zeros(5)                           # [0., 0., 0., 0., 0.]\nnp.ones((2, 3))                       # 2 rows, 3 columns of 1.\nnp.arange(0, 10, 2)                   # [0, 2, 4, 6, 8]\nnp.linspace(0, 1, 5)                  # [0., 0.25, 0.5, 0.75, 1.]\nnp.random.default_rng(0).random(4)    # 4 floats in [0, 1), seeded"},{"t":"p","runs":[{"text":"The seed matters. "},{"text":"default_rng(0)","code":true},{"text":" gives the same “random” numbers every run, which is what you want when a result has to be reproducible. Unseeded, a test that passes today fails tomorrow for no reason you can find."}]},{"t":"p","runs":[{"text":"Every array has three attributes worth printing before you do anything with it:"}]},{"t":"code","lang":"python","label":"Python","text":"arr.shape      # (1000000,)\narr.dtype      # float64\narr.ndim       # 1"},{"t":"h2","id":"vectorised-operations","runs":[{"text":"Vectorised operations"}]},{"t":"p","runs":[{"text":"Arithmetic applies elementwise, and so do comparisons and most maths functions:"}]},{"t":"code","lang":"python","label":"Python","text":"a = np.array([1., 2., 3.])\nb = np.array([10., 20., 30.])\na + b            # [11., 22., 33.]\na * 2            # [2., 4., 6.]\na > 1.5          # [False, True, True]\nnp.sqrt(a)       # elementwise square root\nnp.exp(a)        # elementwise e^x"},{"t":"p","runs":[{"text":"np.sqrt(a)","code":true},{"text":" on an array is not the same as "},{"text":"math.sqrt(a)","code":true},{"text":", which expects one number and raises on an array. The NumPy versions (called "},{"text":"ufuncs","b":true},{"text":") accept arrays and broadcast, which the next lesson covers."}]},{"t":"p","runs":[{"text":"Reductions collapse an array to a number:"}]},{"t":"code","lang":"python","label":"Python","text":"a.sum(), a.mean(), a.max(), a.argmax(), a.std()"},{"t":"p","runs":[{"text":"argmax","code":true},{"text":" returns the "},{"text":"index","i":true},{"text":" of the largest value rather than the value: the thing you want when the array is scores and you need to know which item won."}]},{"t":"h2","id":"the-loop-you-should-not-write","runs":[{"text":"The loop you should not write"}]},{"t":"code","lang":"python","label":"Python","text":"total = 0\nfor x in arr:          # slow: pulls each element out as a Python float\n    total += x"},{"t":"p","runs":[{"text":"Iterating over a NumPy array in Python throws away the advantage; each element is boxed back into a Python object on the way out. If you find yourself writing "},{"text":"for i in range(len(arr))","code":true},{"text":", stop and ask what the whole-array expression is. Usually it exists: a comparison, a "},{"text":"where","code":true},{"text":", a reduction, a dot product. The lesson on masks shows the common ones."}]},{"t":"p","runs":[{"text":"When it does not exist (the computation depends on the previous element in a way no ufunc expresses) the honest options are "},{"text":"numba","code":true},{"text":" (a free just-in-time compiler that makes such loops fast with one decorator) or accepting the loop for a small array."}]},{"t":"h2","id":"when-numpy-does-not-help","runs":[{"text":"When NumPy does not help"}]},{"t":"ul","items":[[{"text":"Small arrays.","b":true},{"text":" For ten numbers, the overhead of calling into C exceeds the loop. NumPy wins from a few hundred elements up."}],[{"text":"Mixed types.","b":true},{"text":" An array holds one dtype. A column of names and prices is not an array; that is pandas’ job."}],[{"text":"Growing one element at a time.","b":true},{"text":" "},{"text":"np.append","code":true},{"text":" copies the whole array each call, so building an array by appending in a loop is quadratic. Collect in a Python list, then "},{"text":"np.array(list)","code":true},{"text":" once at the end."}],[{"text":"Objects.","b":true},{"text":" "},{"text":"np.array([\"a\", \"b\"])","code":true},{"text":" works but gives you fixed-width strings or "},{"text":"object","code":true},{"text":" dtype, and object arrays are Python lists in a NumPy coat: no speed-up."}]]},{"t":"h2","id":"where-this-goes","runs":[{"text":"Where this goes"}]},{"t":"p","runs":[{"text":"Every embedding you fetch from a model is an array of floats, 384 to 3,072 of them. Every batch of them is a two-dimensional array. Similarity search is a matrix multiplication over that array, and the reason it runs over a hundred thousand vectors in milliseconds is exactly the layout described above. Two lessons from now you will write it."}]},{"t":"h2","id":"try-this-now","runs":[{"text":"Try this now"}],"role":"try"},{"t":"p","runs":[{"text":"Build the million-element list and array, time squaring each with "},{"text":"%timeit","code":true},{"text":" or "},{"text":"timeit","code":true},{"text":", and write the ratio down. Then time "},{"text":"sum(nums)","code":true},{"text":" against "},{"text":"arr.sum()","code":true},{"text":", and a Python "},{"text":"for","code":true},{"text":" loop over "},{"text":"arr","code":true},{"text":" against "},{"text":"arr.sum()","code":true},{"text":", to see what iterating an array in Python costs."}]}],"check":{"question":[{"text":"Squaring a million numbers takes 120 ms as a Python list comprehension and 1.5 ms as "},{"text":"arr ** 2","code":true},{"text":" on a NumPy array. A learner explains this as “NumPy uses all the CPU cores”. What is the more accurate account?"}],"options":[{"text":[{"text":"NumPy compiles the expression to machine code at first use and caches it, so later calls skip Python entirely, which is why the second run of any expression is fast and the first is not."}],"correct":false,"tempting":[{"text":"NumPy does not compile expressions; its speed comes from precompiled loops in C that run over contiguous memory."}]},{"text":[{"text":"Python’s interpreter caps loop speed at a fixed number of iterations per millisecond, and NumPy bypasses the interpreter."}],"correct":false,"tempting":[{"text":"There is no such cap; the interpreter is slow per element because of the work each element requires, not a rate limit."}]},{"text":[{"text":"The list version allocates a new list, and allocation dominates; pre-allocating the list would make it as fast as the array."}],"correct":false,"tempting":[{"text":"Pre-allocating helps a little, but the per-element object creation and dispatch remain, and the gap stays close to two orders of magnitude."}]},{"text":[{"text":"The list holds a separate Python object per number, each type-checked and unboxed per iteration; the array holds raw values contiguously and a compiled loop runs over them."}],"correct":true,"tempting":null}],"order":[2,1,0,3],"why":null},"prev":{"slug":"profiling-where-time-goes","title":"Profiling: finding where the time actually goes before you optimise anything","minutes":9},"next":{"slug":"shapes-and-broadcasting","title":"Shapes, axes and broadcasting: the wrong-shape bug that runs without complaint","minutes":9}}}