Python: concatenate generator and item
Posted
by TarGz
on Stack Overflow
See other posts from Stack Overflow
or by TarGz
Published on 2010-03-14T18:39:25Z
Indexed on
2010/03/14
18:45 UTC
Read the original article
Hit count: 201
I have a generator (numbers) and a value (number). I would like to iterate over these as if they were one sequence:
i for i in tuple(my_generator) + (my_value,)
The problem is, as far as I undestand, this creates 3 tuples only to immediately discard them and also copies items in "my_generator" once.
Better approch would be:
def con(seq, item):
for i in seq:
yield seq
yield item
i for i in con(my_generator, my_value)
But I was wondering whether it is possible to do it without that function definition
© Stack Overflow or respective owner