Skip to content

Instantly share code, notes, and snippets.

@BrianHicks
Created December 8, 2014 21:20
Show Gist options
  • Save BrianHicks/8b063def5f66aad46ff5 to your computer and use it in GitHub Desktop.
Save BrianHicks/8b063def5f66aad46ff5 to your computer and use it in GitHub Desktop.
>>> def ints1(lst):
... return [int(x) for x in lst if x.isdigit()]
...
>>> def ints2(lst):
... out = []
... for item in lst:
... try:
... out.append(int(item))
... except ValueError:
... pass
... return out
...
>>> ints1(['a', '1'])
[1]
>>> ints2(['a', '1'])
[1]
>>> import timeit
>>> timeit.timeit(setup='from __main__ import *', stmt='ints1(["1", "2", "a", "b"])')
2.519864082336426
>>> timeit.timeit(setup='from __main__ import *', stmt='ints2(["1", "2", "a", "b"])')
9.285669088363647
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment