Akrios Akrios - 25 days ago 8
Python Question

Convert one-dimensional lists to multi-dimensional lists

For example, I have two lists

a = [1,2,3]
b = [4,5,6]


I want to make a list c which in format of

c = [[1,4],[2,5],[3,6]]


Since I am new to Python, Can anyone help me.
Thank you.

Answer

Use zip (the example code is Python 2 style):

a = [1, 2, 3]
b = [4, 5, 6]
print zip(a, b)
# [(1, 4), (2, 5), (3, 6)]

Python 3 style:

a = [1, 2, 3]
b = [4, 5, 6]
print(list(zip(a, b)))
# [(1, 4), (2, 5), (3, 6)]
Comments