Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.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.114 X-Spam-Level: * X-Spam-Evidence: '*H*': 0.77; '*S*': 0.00; 'variables': 0.07; 'subject:skip:c 10': 0.09; 'cc:addr:python-list': 0.11; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'wrote:': 0.18; 'slightly': 0.19; 'code,': 0.22; 'programming': 0.22; 'cc:addr:python.org': 0.22; 'subject:Code': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; 'am,': 0.29; 'generally': 0.29; 'dec': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'easier': 0.31; '13,': 0.31; 'skip:q 20': 0.31; 'with,': 0.31; 'figure': 0.32; 'fri,': 0.33; 'table': 0.34; "i'd": 0.34; 'point.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'shorter': 0.36; 'subject:List': 0.36; 'generic': 0.38; 'whatever': 0.38; 'short': 0.38; 'skip:p 20': 0.39; 'even': 0.60; 'around.': 0.60; 'name': 0.63; 'sample': 0.67; 'side': 0.67; 'hang': 0.67; 'differently:': 0.84; 'glance': 0.84; 'usage;': 0.84; 'to:none': 0.92; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=SBa03TgXa6Opai1r9DQV8NzJho3bbvN/HSFcePhGaDY=; b=tSMPKZzxwSbqxTQ2eXbXAPc/7pmcatoGJqMGoRYyRcYCW/Y9Arc4tvCUbFX06+zfkb hg9bvhqJQPhTSB113f0vvhyw5xlnUUbajU7zAeyIZcgksmGbsHCeDVhyreXPL33dlBBt Npn8Q/eWMX3OuvTSg1MN0pT15Hwh6g3F/ciF/Ue8xFALfSYmPEF4ipSZZjStoUCFA2E0 Q2aUmnxQCkPVpvuXqdR0AmeUFfXseKU204PtYjcRK+9hwRJhI8nLPFZXTWqJuIwcfjZk ixgP4mTC96OGlWfmsvGq2L2mz3J0Vf4EOG9d5RA128za32LoGndPPg3eqyLvL2O7+YaS OPHQ== MIME-Version: 1.0 X-Received: by 10.66.66.42 with SMTP id c10mr202180pat.98.1386885833992; Thu, 12 Dec 2013 14:03:53 -0800 (PST) In-Reply-To: References: Date: Fri, 13 Dec 2013 09:03:53 +1100 Subject: Re: Code suggestion - List comprehension From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386885838 news.xs4all.nl 2860 [2001:888:2000:d::a6]:57531 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61767 On Fri, Dec 13, 2013 at 7:40 AM, Shyam Parimal Katti wrote: > 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 = '' By the way, side point. It's generally considered good programming practice to use shorter names for short-lived variables and longer names for things that hang around. I'd spell this slightly differently: sample = ['drop table sample_table;', 'create table sample_test', '(col1 int);', 'select col1 from', ' sample_test;'] pure_sqls = [] cur = '' for line in sample: cur += line if cur.endswith(';'): # or line.endswith, or line[-1]==';' pure_sqls.append(cur) cur = '' The short one-token names go with the short usage; the longer name pure_sqls outlives them. Makes it easier to figure out what's important and what's intermediate. The name 'cur' there is debatable; I use it all over the place as a generic accumulator for the "current" whatever I'm working with, you might prefer to use "query" or even "q", take your pick. Makes no difference to the code, but might make it easier for someone to glance over your code and figure out what it's doing. ChrisA