Convert '165.0' to int
rosuav at gmail.com
Mon Jul 25 07:46:22 CEST 2011
On Mon, Jul 25, 2011 at 10:07 AM, Billy Mays <noway at nohow.com> 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). 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