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!
This works as well:
Edit
Could you please comment on the cost tradeoff? (for loop and itertools)Itertools is significantly faster:
Edit 2
Could you pl explain itertools.chain(*)That
*unpacks the sequence into positional arguments, in this case a nested tuple of tuples.Example:
Another example:
See the documents on unpacking.