Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'integers': 0.09; 'it;': 0.09; 'iterate': 0.09; '25,': 0.12; 'am,': 0.13; 'wrote:': 0.15; '(eg': 0.16; 'billy': 0.16; 'evaluates': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'sequence,': 0.16; 'subject:Convert': 0.16; 'suggested.': 0.16; "subject:' ": 0.16; 'mon,': 0.16; 'received:209.85.210.174': 0.19; 'received:mail- iy0-f174.google.com': 0.19; 'memory': 0.21; 'variable': 0.21; 'header:In-Reply-To:1': 0.22; 'personally,': 0.23; 'creating': 0.24; 'index': 0.25; 'function': 0.26; '(and': 0.27; 'message- id:@mail.gmail.com': 0.28; 'actually': 0.33; 'list.': 0.33; 'to:addr:python-list': 0.34; 'however,': 0.34; "can't": 0.34; 'that,': 0.35; 'beginning': 0.36; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'list,': 0.39; 'to:addr:python.org': 0.39; 'received:209': 0.40; "i'd": 0.40; 'tiny': 0.68; 'speed,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=WXsv+KKNCVHsPQL0D9MAc2d+stWsyWNIRaLfo1o+u8Y=; b=NgIcp3Piab6Lm4KlZ+8Fgqwgt5Zqz4mCK5xvRvxNc1s3Hmu6JxKVZSr/e6ojz8op3J FyVcEGmBvztFXNjfGEgc+kdirUfGTUqls96Ke+YI8m8Hor+auS/Yzrx5CVx8zmr2JfKa 8n88bwjErRAVFPRs1gY7oC2j0AOyg+ptAdTxU= MIME-Version: 1.0 In-Reply-To: References: Date: Mon, 25 Jul 2011 15:46:22 +1000 Subject: Re: Convert '165.0' to int From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 17 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1311572784 news.xs4all.nl 23891 [2001:888:2000:d::a6]:54024 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10242 On Mon, Jul 25, 2011 at 10:07 AM, Billy Mays 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. ChrisA