Skip to content

Instantly share code, notes, and snippets.

@benhoyt
Created May 9, 2012 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benhoyt/2647005 to your computer and use it in GitHub Desktop.
Save benhoyt/2647005 to your computer and use it in GitHub Desktop.
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