Tsallaka zuwa abun ciki
Justice Tech School

Vectorising Code

Replace Python loops with array operations for speed and clarity.

Example
import time
a = np.arange(100000)
loop = sum(x * 2 for x in a.tolist())
vec = (a * 2).sum()
print(loop == vec)

Result

True

Exercise

Avoid loops.

Use  operations

Quiz Test

Why vectorise?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.