Code suggestion - List comprehension

Discussion in 'Python' started by Shyam Parimal Katti, Dec 12, 2013.

  1. 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.
     
    Shyam Parimal Katti, Dec 12, 2013
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Barth

    Mix lambda and list comprehension?

    Peter Barth, Jul 15, 2003, in forum: Python
    Replies:
    4
    Views:
    551
    Michele Simionato
    Jul 17, 2003
  2. George Henry

    Strange (?) list comprehension behavior

    George Henry, Jul 19, 2003, in forum: Python
    Replies:
    3
    Views:
    550
    Peter Hansen
    Jul 20, 2003
  3. jsaul

    'while' in list comprehension?

    jsaul, Oct 22, 2003, in forum: Python
    Replies:
    14
    Views:
    961
    Alex Martelli
    Nov 13, 2003
  4. Chris P.
    Replies:
    7
    Views:
    396
    Chris P.
    Jul 20, 2004
  5. Keith Cochrane
    Replies:
    2
    Views:
    835
    Keith Cochrane
    Aug 6, 2006
  6. Shane Geiger
    Replies:
    4
    Views:
    526
    bullockbefriending bard
    Mar 25, 2007
  7. Debajit Adhikary
    Replies:
    17
    Views:
    944
    Debajit Adhikary
    Oct 18, 2007
  8. Vedran Furac(
    Replies:
    4
    Views:
    524
    Marc 'BlackJack' Rintsch
    Dec 19, 2008
Loading...