Using islice and chain.from_iterable :
>>> from itertools import chain, islice >>> elements = [(1,1,1),(2,3,7),(3,5,10)] >>> list(chain.from_iterable(islice(item, 1, 2) for item in elements)) [1, 3, 5]
This can be useful when you need more than one element:
>>> elements = [(0, 1, 2, 3, 4, 5), (10, 11, 12, 13, 14, 15), (20, 21, 22, 23, 24, 25)] >>> list(chain.from_iterable(islice(tuple_, 2, 5) for tuple_ in elements)) [2, 3, 4, 12, 13, 14, 22, 23, 24]
Georgy Jan 27 '19 at 15:27 2019-01-27 15:27
source share