Reputation: 1026
lets assume I have two types of iterables, one of them being a list and the other a generator object (or if it is easier view both as lists)
the list has the following elements: list = [1,2,3,4,5,6,7,8,9]
on the other hand the generator yields a list of tuples in each iteration, the yielded list looks like this: ylist = [(3,"three"),(4,"four"),(5,"five")]
.
I want to be able to get a list which will look like this: finallist = [1,2,(3,"three"),(4,"four"),(5,"five"),6,7,8,9]
.
How do I achieve this?
Upvotes: 1
Views: 135
Reputation: 251096
Convert the return items from that generator to a dict and then use a list comprehension:
>>> lis = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> ylist = [(3, "three"),(4, "four"),(5, "five")]
>>> dic = dict(ylist)
>>> [x if x not in dic else (x, dic[x]) for x in lis]
[1, 2, (3, 'three'), (4, 'four'), (5, 'five'), 6, 7, 8, 9]
Upvotes: 4