Eliminating loops

Writing Efficient Python Code

Logan Thomas

Scientific Software Technical Trainer, Enthought

Looping in Python

  • Looping patterns:
    • for loop: iterate over sequence piece-by-piece
    • while loop: repeat loop as long as condition is met
    • "nested" loops: use one loop inside another loop
    • Costly!
Writing Efficient Python Code

Benefits of eliminating loops

  • Fewer lines of code
  • Better code readability
    • "Flat is better than nested"
  • Efficiency gains
Writing Efficient Python Code

Eliminating loops with built-ins

# List of HP, Attack, Defense, Speed
poke_stats = [
    [90,  92, 75, 60],
    [25,  20, 15, 90],
    [65, 130, 60, 75],
    ...
]

alt=”The Pokémon named Abomasnow, Abra, and Absol and their corresponding Health Points, Attack, Defense, and Speed metadata highlighted”

Writing Efficient Python Code
# List of HP, Attack, Defense, Speed
poke_stats = [
    [90,  92, 75, 60],
    [25,  20, 15, 90],
    [65, 130, 60, 75],
    ...
]

# For loop approach totals = [] for row in poke_stats: totals.append(sum(row))
# List comprehension totals_comp = [sum(row) for row in poke_stats]
# Built-in map() function totals_map = [*map(sum, poke_stats)]
Writing Efficient Python Code
%%timeit
totals = []
for row in poke_stats:
    totals.append(sum(row))
140 µs ± 1.94 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit totals_comp = [sum(row) for row in poke_stats]
114 µs ± 3.55 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit totals_map = [*map(sum, poke_stats)]
95 µs ± 2.94 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Writing Efficient Python Code

Eliminating loops with built-in modules

poke_types = ['Bug', 'Fire', 'Ghost', 'Grass', 'Water']
# Nested for loop approach
combos = []
for x in poke_types:
    for y in poke_types:
        if x == y:
            continue
        if ((x,y) not in combos) & ((y,x) not in combos):
            combos.append((x,y))
# Built-in module approach
from itertools import combinations
combos2 = [*combinations(poke_types, 2)]
Writing Efficient Python Code

Eliminate loops with NumPy

# Array of HP, Attack, Defense, Speed
import numpy as np

poke_stats = np.array([
    [90,  92, 75, 60],
    [25,  20, 15, 90],
    [65, 130, 60, 75],
    ...
])
Writing Efficient Python Code

Eliminate loops with NumPy

avgs = []
for row in poke_stats:
    avg = np.mean(row)
    avgs.append(avg)

print(avgs)
[79.25, 37.5, 82.5, ...]
avgs_np = poke_stats.mean(axis=1)

print(avgs_np)
[ 79.25  37.5   82.5  ...]
Writing Efficient Python Code

Eliminate loops with NumPy

%timeit avgs = poke_stats.mean(axis=1)
23.1 µs ± 235 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%%timeit
avgs = []
for row in poke_stats:
    avg = np.mean(row)
    avgs.append(avg)
5.54 ms ± 224 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Writing Efficient Python Code

Let's practice!

Writing Efficient Python Code

Preparing Video For Download...