sokacoding
May 6, 2023

Thanks for sharing these insights! You inspired me to dive a bit deeper. I checked both functions using the timeit() function with different numbers of iterations. In terms of execution time both approaches are very similar with list comprehension being slightly faster. I repeated the measurement with a fixed iteration number of 1000, increasing the text size and looking at speed and memory usage (using psutil). Long story short: When your data is getting bigger, use generators!

Plot: https://imgur.com/a/Pmnt3om

sokacoding
sokacoding

Written by sokacoding

M.Sc. Media Informatics. Scientific Associate at Hochschule Düsseldorf - University of Applied Sciences.

Responses (2)