With Sqlite, a \"select..from\" command returns the results \"output\", which prints (in python):
>>print output
[(12.2817, 12.2817), (0, 0), (8.52, 8.
Update: Flattening using extend but without comprehension and without using list as iterator (fastest)
After checking the next answer to this that provided a faster solution via a list comprehension with dual for
I did a little tweak and now it performs better, first the execution of list(...) was dragging a big percentage of time, then changing a list comprehension for a simple loop shaved a bit more as well.
The new solution is:
l = []
for row in output: l.extend(row)
Older:
Flattening with map/extend:
l = []
list(map(l.extend, output))
Flattening with list comprehension instead of map
l = []
list(l.extend(row) for row in output)
some timeits for new extend and the improvement gotten by just removing list(...) for [...]:
import timeit
t = timeit.timeit
o = "output=list(zip(range(1000000000), range(10000000))); l=[]"
steps_ext = "for row in output: l.extend(row)"
steps_ext_old = "list(l.extend(row) for row in output)"
steps_ext_remove_list = "[l.extend(row) for row in output]"
steps_com = "[item for sublist in output for item in sublist]"
print("new extend: ", t(steps_ext, setup=o, number=10))
print("old extend w []: ", t(steps_ext_remove_list, setup=o, number=10))
print("comprehension: ", t(steps_com, setup=o, number=10,))
print("old extend: ", t(steps_ext_old, setup=o, number=10))
>>> new extend: 4.502427191007882
>>> old extend w []: 5.281140706967562
>>> comprehension: 5.54302118299529
>>> old extend: 6.840151469223201
use itertools
chain:
>>> import itertools
>>> list(itertools.chain.from_iterable([(12.2817, 12.2817), (0, 0), (8.52, 8.52)]))
[12.2817, 12.2817, 0, 0, 8.52, 8.52]
>>> flat_list = []
>>> nested_list = [(1, 2, 4), (0, 9)]
>>> for a_tuple in nested_list:
... flat_list.extend(list(a_tuple))
...
>>> flat_list
[1, 2, 4, 0, 9]
>>>
you could easily move from list of tuple to single list as shown above.