<div dir="ltr">I believe you want something like<div><br></div><div>comm = [row[0] for row in c.fetchall()]</div><div><br></div><div>Keep in mind that the list comprehension is going to evaluate that part before the "for" for each row it gets from the cursor, so you need the [0] inside there, not outside the list comprehension, which would be asking for the first element from the list comprehension—not what you want.</div><div><br></div><div>Note that c.fetchall() fetches all rows, which might be a waste of memory for large data sets.  Psycopg (PostgreSQL DB API library) lets you iterate over a cursor directly:</div><div><br></div><div>comm = [row[0] for row in c]</div><div><br></div><div><i>In theory</i> this doesn't have to load all the rows into memory at once.  (In practice I bet it does unless you do something special with Psycopg/PostgreSQL.)  I'm not sure if you can just iterate over a cursor with your MySQL driver, but there's no harm in trying.</div><div><br></div><div>Dale</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, May 25, 2016 at 10:38 AM, Dan Mahoney <span dir="ltr"><<a href="mailto:catdude@gmail.com" target="_blank">catdude@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div><div><div><div><div><div><div>I've got a question about using list comprehensions.<br><br></div>I've got a piece of code that is making a MySQL query:<br>sql = "SELECT tail FROM aircraft_state WHERE airline='WN'"<br></div>c.execute(sql)<br><br></div>I'm currently using a loop to get the values into a list:<br></div>rows = c.fetchall()<br></div>for row in rows:<br></div>    comm = row[0]<br></div><br></div>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:<br><br></div>comm = [x for x in c.fetchall()][0]<br><br></div>but that just gives me the first tuple. If I use:<br><br></div>comm = [s for x in c.fetchall()]<br><br></div>I end up with a list of tuples. Any suggestions as to how I could do this without using a for loop?<span class="HOEnZb"><font color="#888888"><br><br clear="all"><div><div><div><div><div><div><div><div><div><div><div><div><div><br>-- <br><div><div dir="ltr"><div><div dir="ltr"><div>--------------------------<br>Dan Mahoney<br><a href="mailto:catdude@gmail.com" target="_blank">catdude@gmail.com</a><br></div><div>Skype: catdude60440<br></div><div><br>"How you behave towards cats here below determines your status in Heaven."<br>Robert Heinlein<br><br>"There are two means of refuge from the miseries of<br>life - music and cats" - Albert Schweitzer</div></div></div></div></div>
</div></div></div></div></div></div></div></div></div></div></div></div></div></font></span></div>
<br>_______________________________________________<br>
Chicago mailing list<br>
<a href="mailto:Chicago@python.org">Chicago@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/chicago" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/chicago</a><br>
<br></blockquote></div><br></div>