I'm getting an iterable of tuples as a result from a sqlite3 select statement, and I want to give this iterable to a function that expects a string iterable. How can I override the next function to give the first index of the tuple? Or to be more precise, what is the right pythonic way of doing this?
>>> res = conn.execute(query,(font,))
>>> train_counts = count_vect.fit_transform(res)
AttributeError: 'tuple' object has no attribute 'lower'
EDIT:
Since mapping involves iterating over the entire list it takes twice as much time as just constructing a generator as Niklas offered.
first = """
l = list()
for i in xrange(10):
l.append((i,))
for j in (i[0] for i in l):
j
"""
second = """
l = list()
for i in xrange(10):
l.append((i,))
convert_to_string = lambda t: "%d" % t
strings = map(convert_to_string, l)
for j in strings:
j
"""
third = """
l = list()
for i in xrange(10):
l.append((i,))
strings = [t[0] for t in l]
for j in strings:
j
"""
print "Niklas B. %f" % timeit.Timer(first).timeit()
print "Richard Fearn %f" % timeit.Timer(second).timeit()
print "Richard Fearn #2 %f" % timeit.Timer(third).timeit()
>>>
Niklas B. 4.744230
Richard Fearn 12.016272
Richard Fearn #2 12.041094
mapin Python 3 is just as lazy as a generator expression :)