Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'example:': 0.03; 'function,': 0.07; 'ugly': 0.07; 'python': 0.09; '22,': 0.09; 'subject:into': 0.09; 'anyway': 0.11; 'extension': 0.13; 'resulting': 0.13; 'is).': 0.16; 'iterator': 0.16; 'map(int,': 0.16; 'reason.': 0.16; 'sequence.': 0.16; 'subject:Converting': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'changes': 0.20; 'file.': 0.20; 'int,': 0.22; 'cheers,': 0.23; 'pass': 0.25; 'header:In-Reply- To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'subject:list': 0.28; 'comparison': 0.29; 'overhead': 0.29; "i'm": 0.29; 'call.': 0.30; 'performing': 0.30; 'function': 0.30; 'code': 0.31; '(and': 0.32; 'to:addr:python-list': 0.33; 'everyone': 0.33; 'typically': 0.33; 'hi,': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'list': 0.35; 'built-in': 0.35; 'returning': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'anything': 0.36; 'does': 0.37; 'option': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'instead': 0.39; 'performance': 0.39; 'to:addr:python.org': 0.39; 'list,': 0.39; 'header:Received:5': 0.40; 'help': 0.40; 'end': 0.40; 'remove': 0.61; 'further': 0.61; 'map': 0.61; 'situation': 0.62; 'between': 0.63; 'more': 0.63; 'choose': 0.65; 'jul': 0.65; 'compose': 0.84; 'premature': 0.84; 'quicker': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=2+kboL1yKb1ZGH89zkasKjXpWvZORO2d70Ii/52fRi0=; b=xMze6a2+G1cj1WPX5cz2arxj6bdDYgiJROcAuspWvHX0RvNH9DovpWfZ0v1xIu1Dza /hC+ljUa1YiZUB1IhjxcklHnlZF0pBkehcYD/D+/9FlbtXdvsNCUQPnA/0dvqUECUung eoLfdYVpFvBIA1CqmiysgleLCaoiPcR8UwXY2ZXCutDjxetDYIYMiOyAdvn5Bzc0dMNd F3GgPfeGZg3fzY1ruJQaFq2Cw/Dl154nZuhpHEBAl6pf9XLbH/bXQay4TZMMY9X3bhFX s4DWl+qnxB4oRCXdvx+2FAZohrC/muvFZipw3k+81TjaExkwFK08tkTPNRs7mSp39XDX elGg== MIME-Version: 1.0 In-Reply-To: <500C2842.4040204@freenet.de> References: <3rCdnUCiWpP1gZHNnZ2dnUVZ7vQAAAAA@giganews.com> <500C2842.4040204@freenet.de> From: Ian Kelly Date: Sun, 22 Jul 2012 11:16:26 -0600 Subject: Re: Converting a list of strings into a list of integers? To: Python 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342977424 news.xs4all.nl 6847 [2001:888:2000:d::a6]:52046 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25825 On Sun, Jul 22, 2012 at 10:20 AM, Jan Riechers wrote: > Hi, > > I am not sure why everyone is using the for-iterator option over a "map", > but I would do it like that: > > MODUS_LIST= map(int, options.modus_list) > > "map" works on a list and does commandX (here "int" conversion, use "str" > for string.. et cetera) on sequenceY, returning a sequence. More in the help > file. > > And if I'm not completely mistaken, it's also the quicker way to do > performance wise. But I can't completely recall the exact reason. My recollection is that map has the edge if you can pass it a built-in or a C extension function, like int, or a complicated Python function that you would end up calling anyway in the list comprehension. The situation changes though if you can write the comprehension to remove the overhead of the Python function call. For example: map(lambda x: x+1, my_list) [x+1 for x in my_list] By performing the addition inline instead of calling a function to do it, the list comprehension wins performance-wise in this scenario. So as a simple rule of thumb I will typically choose between map or a comprehension based on whether I need to call a function or not (and also based on how pretty or ugly the resulting code is). Anything further would just be premature optimization. Also keep in mind that in Python 3 map returns an iterator instead of a list, so for a fair comparison you would have to compose the map with a list() call. Cheers, Ian