You can combine some ideas from alecx's answer and what you already had:
>>> import itertools >>> >>> a = [1, 5, 7] >>> b = ((x, x+1) for x in a) >>> >>> list(itertools.chain(*b)) [1, 2, 5, 6, 7, 8]
What I've done:
Define an expression b a that allows us to have a (something similar) tuple that would look like ((1, 2), (5, 6), (7, 8)) , but without an estimate right away. He would also work with a list.
Unpack b in the argument list itertools.chain (). This would be equivalent to itertools.chain((1, 2), (5, 6), (7, 8)) . This function combines its arguments.
Use list () to create a list from the return value of the itertools.chain() function, since it is an iterator.
This could also work without an intermediate step:
>>> import itertools >>> list(itertools.chain(*((x, x+1) for x in [1, 5, 7]))) [1, 2, 5, 6, 7, 8]
But "Simple is better than complex."
Hope this helps.
I would put more links if I had a great reputation, sorry.
source share