Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'decent': 0.07; 'received:verizon.net': 0.07; 'terry': 0.07; 'python': 0.08; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'subject:Extracting': 0.09; 'skip:[ 20': 0.12; "'w')": 0.16; 'compact': 0.16; 'fin:': 0.16; 'guys,': 0.16; 'incomplete': 0.16; 'iterator': 0.16; 'omitted.': 0.16; 'reedy': 0.16; 'throwaway': 0.16; 'versus': 0.16; "wouldn't": 0.16; 'wrote:': 0.18; 'example.': 0.18; 'lines,': 0.18; 'jan': 0.19; 'seems': 0.19; 'issue.': 0.21; 'header:In-Reply-To:1': 0.23; 'code': 0.25; 'creating': 0.25; 'writes:': 0.25; 'skip:[ 10': 0.27; 'not.': 0.27; 'bit': 0.28; 'looks': 0.28; 'collections': 0.30; 'subject:?': 0.31; 'thanks': 0.31; 'pm,': 0.31; 'coding': 0.32; 'used,': 0.32; 'does': 0.32; 'list': 0.32; 'to:addr:python-list': 0.32; 'actually': 0.33; 'instead': 0.33; 'header:User-Agent:1': 0.33; 'source': 0.33; 'header:X-Complaints-To:1': 0.33; 'loop': 0.34; 'elegant': 0.34; 'rather': 0.35; 'subject:lists': 0.36; 'fair': 0.36; 'but': 0.37; 'received:org': 0.37; 'could': 0.38; 'enough': 0.38; 'why': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; 'john': 0.62; 'become': 0.69; '100': 0.69; 'subject:over': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Extracting elements over multiple lists? Date: Mon, 07 Nov 2011 19:06:53 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 In-Reply-To: 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1320710828 news.xs4all.nl 6858 [2001:888:2000:d::a6]:52279 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15445 On 11/7/2011 1:22 PM, John Gordon wrote: > In JoeM writes: > >> Thanks guys, I was just looking for a one line solution instead of a >> for loop if possible. Why do you consider > >> [x.remove(x[0]) for x in [a,b,c]] > >> cheating? It seems compact and elegant enough for me. It looks like incomplete code with 'somelists = ' or other context omitted. It saves no keypresses '[',...,SPACE,...,']' versus ...,':',ENTER,TAB,... . (TAB with a decent Python aware editor.) > I wouldn't call it cheating, but that solution does a fair bit of > unneccessary work (creating a list comprehension that is never used.) The comprehension ( the code) is used, but the result is not. If the source iterator has a large number of items rather than 3, the throwaway list could become an issue. Example. fin = open('source.txt') fout= open('dest.txt, 'w') for line in fin: fout.write(line.strip()) # versus [fout.write(line.strip()) for line in fin] If source.txt has 100 millions lines, the 'clever' code looks less clever ;=). Comprehensions are intended for creating collections (that one actually wants) and for normal Python coding are best used for that. -- Terry Jan Reedy