[Chicago] List comprehension question

Dan Mahoney catdude at gmail.com
Wed May 25 11:38:10 EDT 2016


I've got a question about using list comprehensions.

I've got a piece of code that is making a MySQL query:
sql = "SELECT tail FROM aircraft_state WHERE airline='WN'"
c.execute(sql)

I'm currently using a loop to get the values into a list:
rows = c.fetchall()
for row in rows:
    comm = row[0]

since the return value from MySQL is delivered as tuples. I'd like to use a
list comprehension to build my final list (no important reason, I just want
to learn how). I tried:

comm = [x for x in c.fetchall()][0]

but that just gives me the first tuple. If I use:

comm = [s for x in c.fetchall()]

I end up with a list of tuples. Any suggestions as to how I could do this
without using a for loop?


-- 
--------------------------
Dan Mahoney
catdude at gmail.com
Skype: catdude60440

"How you behave towards cats here below determines your status in Heaven."
Robert Heinlein

"There are two means of refuge from the miseries of
life - music and cats" - Albert Schweitzer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20160525/0cdccecc/attachment.html>


More information about the Chicago mailing list