How can I fetch mysql single column list results directly into a list in python? -


i have mysql call:

zip = 48326  cursor.execute ("""     select distinct(name)        usertab       zip= %s """ , (zip))  result = cursor.fetchall() 

the result returned in tuple looks this:

result = (('alan',), ('bob',), ('steve',), ('tom',)) 

but need list this:

mylist= ['alan', 'bob', 'steve', 'tom'] 

so process tuple list this:

mylist = []  row, key in enumerate(result):     col, data in enumerate(key):         mylist.append(data) 

this code works, i'd simpler way.
how can fetch mysql single column result directly list?

do that, using list comprehension:

mylist = [row[0] row in result] 

it flatten result , create list it.

another approach, without list comprehension:

from itertools import chain mylist = list(chain.from_iterable(result)) 

it flatten result , convert list.


Comments