1

I'm sure that this is pretty easy to do but I'm not sure how to do it.

I have three lists: list1=[a1, a2, a3...], list2=[b1, b2, b3...], and list3=[c1, c2, c3...]

I want to pass a list that is a by-item merger of these three:

finalList = [[a1, b1, c1,], [a2, b2, c2], [a3, b3, c3]...]

How do I do this?

1 Answer 1

8

That's what the built-in zip() is for:

final_list = zip(list1, list2, list3)

Note that final_list will actually be a list of tuples, and its length will be the length of the shortest input list.

The functions itertools.izip() and itertools.izip_longest() are also noteworthy.

Sign up to request clarification or add additional context in comments.

1 Comment

ah awesome, thanks. additional q: if I'm going to be iterating through final_list in a template, is there an obvious way to do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.