Writing better loops

Writing Efficient Python Code

Logan Thomas

Scientific Software Technical Trainer, Enthought

Lesson caveat

  • Some of the following loops can be eliminated with techniques covered in previous lessons.

  • Examples in this lesson are used for demonstrative purposes.

alt=”Warning sign with text Warning: for demonstration purposes only”

Writing Efficient Python Code

Writing better loops

  • Understand what is being done with each loop iteration
  • Move one-time calculations outside (above) the loop
  • Use holistic conversions outside (below) the loop
  • Anything that is done once should be outside the loop
Writing Efficient Python Code

Moving calculations above a loop

import numpy as np

names = ['Absol', 'Aron', 'Jynx', 'Natu', 'Onix']
attacks = np.array([130, 70, 50, 50, 45])

for pokemon,attack in zip(names, attacks):
total_attack_avg = attacks.mean()
if attack > total_attack_avg: print( "{}'s attack: {} > average: {}!" .format(pokemon, attack, total_attack_avg) )
Absol's attack: 130 > average: 69.0!
Aron's attack: 70 > average: 69.0!
Writing Efficient Python Code
import numpy as np

names = ['Absol', 'Aron', 'Jynx', 'Natu', 'Onix']
attacks = np.array([130, 70, 50, 50, 45])

# Calculate total average once (outside the loop) total_attack_avg = attacks.mean()
for pokemon,attack in zip(names, attacks): if attack > total_attack_avg: print( "{}'s attack: {} > average: {}!" .format(pokemon, attack, total_attack_avg) )
Absol's attack: 130 > average: 69.0!
Aron's attack: 70 > average: 69.0!
Writing Efficient Python Code

Moving calculations above a loop

%%timeit
for pokemon,attack in zip(names, attacks):

    total_attack_avg = attacks.mean()

    if attack > total_attack_avg:
        print(
            "{}'s attack: {} > average: {}!"
            .format(pokemon, attack, total_attack_avg)
        )
74.9 µs ± 3.42 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Writing Efficient Python Code

Moving calculations above a loop

%%timeit
# Calculate total average once (outside the loop)
total_attack_avg = attacks.mean()

for pokemon,attack in zip(names, attacks):

    if attack > total_attack_avg:
        print(
            "{}'s attack: {} > average: {}!"
            .format(pokemon, attack, total_attack_avg)
        )
37.5 µs ± 281 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Writing Efficient Python Code

Using holistic conversions

names = ['Pikachu', 'Squirtle', 'Articuno', ...]
legend_status = [False, False, True, ...]
generations = [1, 1, 1, ...]

poke_data = [] for poke_tuple in zip(names, legend_status, generations):
poke_list = list(poke_tuple)
poke_data.append(poke_list)
print(poke_data)
[['Pikachu', False, 1], ['Squirtle', False, 1], ['Articuno', True, 1], ...]
Writing Efficient Python Code

Using holistic conversions

names = ['Pikachu', 'Squirtle', 'Articuno', ...]
legend_status = [False, False, True, ...]
generations = [1, 1, 1, ...]

poke_data_tuples = [] for poke_tuple in zip(names, legend_status, generations): poke_data_tuples.append(poke_tuple)
poke_data = [*map(list, poke_data_tuples)]
print(poke_data)
[['Pikachu', False, 1], ['Squirtle', False, 1], ['Articuno', True, 1], ...]
Writing Efficient Python Code
%%timeit
poke_data = []
for poke_tuple in zip(names, legend_status, generations):
    poke_list = list(poke_tuple)
    poke_data.append(poke_list)
261 µs ± 23.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%%timeit
poke_data_tuples = []
for poke_tuple in zip(names, legend_status, generations):
    poke_data_tuples.append(poke_tuple)

poke_data = [*map(list, poke_data_tuples)]
224 µs ± 1.67 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Writing Efficient Python Code

Time for some practice!

Writing Efficient Python Code

Preparing Video For Download...