Convert '165.0' to int

Chris Angelico rosuav at
Mon Jul 25 01:46:22 EDT 2011

On Mon, Jul 25, 2011 at 10:07 AM, Billy Mays <noway at> wrote:
> if the goal is speed, then you should use generator expressions:
> list_of_integers = (int(float(s)) for s in list_of_strings)

Clarification: This is faster if and only if you don't actually need
it as a list. In spite of the variable name, it's NOT a list, and you
can't index it (eg you can't work with list_of_integers[7]). However,
you can iterate over it to work with the integers in sequence, and for
that specific (and very common) use, it will be faster and use less
memory than actually creating the list. It's also going to be a LOT
faster than creating the list, if you only need a few from the
beginning of it; the generator evaluates lazily.

Personally, I'd just create a tiny function and use that, as has been suggested.


More information about the Python-list mailing list