John
John

Reputation: 2147

Efficient way to zip a list with lists nested in another list in python

if we want to zip a list with lists nested a another list e.g.

a = [1,2,3] 
b = [ ['a', 'b', 'c'], ['1', '2', '3']  ]
result = [ (1,'a', '1'), (2,'b','2'), (3,'c','3') ]

how can it be done efficiently giving the sizes of the lists can be huge? (i.e. we don't want to do b.append(c) beforehand then zip). thanks

Upvotes: 0

Views: 2267

Answers (1)

falsetru
falsetru

Reputation: 369054

Use * operator (See Python tutorial - Unpacking Argument Lists)

>>> a = [1,2,3]
>>> b = [ ['a', 'b', 'c'], ['1', '2', '3']  ]
>>> zip(a, *b)
[(1, 'a', '1'), (2, 'b', '2'), (3, 'c', '3')]

Upvotes: 2

Related Questions