Efficient Python part 3: Gaining efficiency

Cássio Bolba
5 min readAug 23, 2022

Welcome to the third part out of four in the series on how to write more efficient python code. In the first part, we checked the very basics: Pythonic code, built-in functions, and just a bit on how numpy can improve speed. In the second part we learned how to measure which code is faster and enable comparison of efficiency. As a recap, the series comprehends:

Combining, counting and iterating

let’s say we have a list os pokemons and we want to combine:

names = ['Bulbasaur','Charmander','Squirtle']
hps = [45,39,44]
combined = []for i,pokemon in enumerate(names):
combined.append((pokemon,hps[i]))
print(combined)

But this is not elegant and efficient.
Then, use Zip:

names = ['Bulbasaur','Charmander','Squirtle']
hps = [45,39,44]
combined_zip = zip(names, hps)
# combined zip is packed, then use * to unpack
combined_zip_list [*combined_zip]
print(combined_zip_list)

Collection Module

--

--

Cássio Bolba
Cássio Bolba

Written by Cássio Bolba

Senior Data Engineer | Udemy Teacher | Expat in Germany | Mentor -> https://linktr.ee/cassiobolba

No responses yet