How do I convert tuple of tuples to list in one line (pythonic)?

Posted by ThinkCode on Stack Overflow See other posts from Stack Overflow or by ThinkCode
Published on 2011-11-17T17:40:54Z Indexed on 2011/11/17 17:51 UTC
Read the original article Hit count: 369

Filed under:
|
|
|
|
query = 'select mydata from mytable'
cursor.execute(query)
myoutput = cursor.fetchall() 
print myoutput

(('aa',), ('bb',), ('cc',))

Why is it (cursor.fetchall) returning a tuple of tuples instead of a tuple since my query is asking for only one column of data?

What is the best way of converting it to ['aa', 'bb', 'cc'] ?

I can do something like this :

mylist = []
myoutput = list(myoutput)
for each in myoutput:
   mylist.append(each[0])

I am sure this isn't the best way of doing it. Please enlighten me!

© Stack Overflow or respective owner

Related posts about python

Related posts about list