Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'explicit': 0.07; 'string': 0.09; "ain't": 0.09; 'indicates': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:c 10': 0.09; 'language.': 0.14; 'eliminating': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'elements': 0.16; 'language': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'trying': 0.19; 'split': 0.19; 'code,': 0.22; 'header:User-Agent:1': 0.23; 'subject:Code': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'appear': 0.29; 'skip:p 30': 0.29; 'code': 0.31; 'piece': 0.31; 'skip:q 20': 0.31; 'table': 0.34; 'maybe': 0.34; 'there': 0.35; 'version': 0.36; 'shorter': 0.36; 'subject:List': 0.36; 'done': 0.36; 'list': 0.37; 'expected': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'conversion': 0.61; 'new': 0.61; 'such': 0.63; 'our': 0.64; 'temporary': 0.65; 'here': 0.66; 'sample': 0.67; 'received:89': 0.85 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Code suggestion - List comprehension Date: Thu, 12 Dec 2013 21:55:55 +0000 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-89-240-164-174.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386885377 news.xs4all.nl 2923 [2001:888:2000:d::a6]:46654 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61761 On 12/12/2013 20:40, Shyam Parimal Katti wrote: > Hello, > > I have a list of sql queries, some which are split across multiple list > elements e.x. > ['drop table sample_table;', 'create table sample_test', '(col1 int);', > 'select col1 from', ' sample_test;'] > > A semi-colon in the string value indicates the termination of a sql > query. So the expected out come is a conversion to a list of valid sql > queries: > ['drop table sample_table;', 'create table sample_test (col1 int);', > 'select col1 from sample_test;'] > > Here is the code that does that: > > sample = ['drop table sample_table;', 'create table sample_test', '(col1 > int);', 'select col1 from', ' sample_test;'] > pure_sqls = [] > query_holder= '' > for each_line in sample: > query_holder += each_line > if query_holder.endswith(';'): > pure_sqls.append(query_holder) > query_holder = '' > > > Is there a way to do this by eliminating explicit creation of new > list(pure_sqls) and a temporary variable(query_holder)? Using list > comprehension? Though I don't want to put the shorter version in > production(if it is difficult to understand), I am looking if this can > be done with list comprehension since I am trying to learn list > comprehension by using it in such scenarios. > I don't think this can be done with a list comprehension. As you appear to have a perfectly good piece of code, if it ain't broke, don't fix it :) Maybe change one line. if query_holder[-1] == ';': -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence