Palindrome

Francis Avila francisgavila at yahoo.com
Thu Nov 13 13:11:33 EST 2003


"Pierre Quentel" <quentel.pierre at wanadoo.fr> wrote in message
news:mailman.715.1068739302.702.python-list at python.org...
> To remove the characters that are not alphanumeric I would have used
> filter :
>
> t=filter(lambda x: x.isalnum(),list(s.lower()))

Why not the more readable list comprehension?

t = [c for c in s.lower() if c.isalnum()]

It's even a tiny bit faster:

...>python lib/timeit.py -s"s='Go Hang a Salami! I\'m a Lasagna Hog!'"
"t=filter(lambda x: x.isalnum(),list(s.lower()))"
10000 loops, best of 3: 91.6 usec per loop

...>python lib/timeit.py -s"s='Go Hang a Salami! I\'m a Lasagna Hog!'" "t =
[c for c in s.lower() if c.isalnum()]"
10000 loops, best of 3: 80.3 usec per loop

--
Francis Avila





More information about the Python-list mailing list