Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Speed of list comprehension vs for loop with append
# On my Windows 7 64-bit machine running CPython 2.6
C:\>python -m timeit -s "lst = [1] * 100" "out = [x for x in lst if x]"
100000 loops, best of 3: 6.55 usec per loop
C:\>python -m timeit -s "lst = [1] * 100" "out = []" "for x in lst:" " if x:" " out.append(x)"
100000 loops, best of 3: 14.7 usec per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.