Reputation: 1977
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.
Upvotes: 0
Views: 61
Reputation: 6438
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)]
Upvotes: 4
Reputation: 4730
If you actually want internal lists instead of tuples you can use:
a = [1,2,3]
b = [4,5,6]
c = [list(result) for result in zip(a,b)]
# c = [[1,4],[2,5],[3,6]]
Upvotes: 1