I am using Python 2.7 to turn a tuple of lists (of 2 elements) of float into a list of floats (([1.0],[2.0])
=> [1.0,2.0]
) like the following:
[tuple_lists[0][0],tuple_lists[1][0]]
Is there a pythonic 2.7 way to do so in a more elegant way?
If there is exactly one item in all the lists in the tuple, you can more idiomatically unpack them in a list comprehension:
[i for i, in tuple_lists]
You can also use chain.from_iterable
to join the lists:
from itertools import chain
list(chain.from_iterable(tuple_lists))
Or you can map the sequence to operator.itemgetter(0)
:
from operator import itemgetter
map(itemgetter(0), tuple_lists)
Zipping the lists of 1 item bundles them together as a tuple, though you'd have to access the tuple with an index of 0:
list(zip(*tuple_lists)[0])
Since in your case there are only two lists in the tuple, you can also simply use the add
operator to join them:
from operator import add
add(*tuple_lists)
Demo here