Autarchy of the Private Cave

Tiny bits of bioinformatics, [web-]programming etc

    Elegantly converting Python list into another list with changed items

    30th August 2007

    I just had a small problem with my Python script:

    1. I have a list of tuples, called records_cache; each tuple looks like this:
    (note: for copy-pasting, click the PLAIN TEXT box headers)

    1. (ensembl_transcript_id, ensembl_gene_id, chrom_start, chrom_end, utr_start, utr_end, sequence, strand)

    2. I need to INSERT only some of those values into MySQL table, using the MySQLdb executemany() method. The values I need are utr_start, utr_end, ensembl_gene_id (in exactly this order).

    To do this, I need to create another list of tuples, but with shorter 3-item tuples. Let us call the new list genes_update.

    Here is the elegant, though probably not the most efficient, solution:

    1. genes_update = [(onetuple[4],onetuple[5],onetuple[1]) for onetuple in records_cache]]

    Let’s see what is happening here:

    1. First, note the brackets around the r-expression. These brackets force the result to be a list.
    2. Next, note the construct used, it is from the Python manual: list_element.method() for list_element in list. This construct iterates all items of the list, in our case – of the records_cache list. This construct also applies the list_element.method() to each list method.
    3. Instead of applying some list_element.method(), I just use tuple indices to select the values I need, and then (using parenthesis) join those values into a tuple again.
    4. So for each tuple in the records_cache list, another (shorter) tuple is returned, and those shorter tuples are joined into the new list, which is assigned to the genes_update list.

    After this conversion, I can happily write that list of tuples into MySQL table, using cursor.executemany():
    (note: error_log() is a small custom function for appending to script’s error.log file)

    1. try:
    2.     cursor.executemany("""UPDATE genes SET
    3.        5utr_start = %s,
    4.        5utr_end = %s
    5.        WHERE ensembl_gene_id = %s""", genes_update)
    6.     db.commit()
    7. except MySQLdb.Error, (errno, strerror):
    8.     db.rollback()
    9.     error_log("Error %d: %s (%s)\n" % (errno, strerror, insertq))
    10.     return False # failed to update
    11. return True # updated

    Comments are welcome, especially on the efficiency improvement.

    Share

    One Response to “Elegantly converting Python list into another list with changed items”

    1. Wahoo Says:

      Thank you for sharing!

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>