anhtran
anhtran

Reputation: 2044

Python: Make new tuple by attaching info from existing list

I have 2 lists:

l = [1,2,3,4,5,6,7]
f = [[3,'Red'],[2,'Blue']]

I want to do with l based on f so the result will be something like:

result = ((1,'Red'), (2,'Red'), (3,'Red'), (4,'Blue'), (5,'Blue'), (6,'None'), (7,'None'))

Could you give me some code to do this simple and easy? Thanks for any help!

Upvotes: 1

Views: 116

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798746

result = tuple(itertools.izip_longest(l, (x[1] for x in f for y in range(x[0])), fillvalue='None'))

Upvotes: 9

Related Questions