-4

For example, if I have:

A = [1, 2, 3]` & `B = [4, 5, 6]

and I would like to have:

C = [[1, 4], [2, 5], [3, 6]]
Ann Zen
  • 25,080
  • 7
  • 31
  • 51

4 Answers4

1

You can use tuple and zip to meet this requirement.

Sample code -

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

>>> c = tuple(zip(a,b))
>>> print(c)
((1, 4), (2, 5), (3, 6))
Shantanu Kher
  • 914
  • 1
  • 7
  • 13
1

There is a builtin function called zip for this:

[list(ab) for ab in zip(a,b)]

Or using map and zip:

list(map(list, zip(a,b)))

Both return:

[[1, 4], [2, 5], [3, 6]]
Jab
  • 25,138
  • 21
  • 72
  • 111
1

You can do this:

a = [1,2,3]
b = [4,5,6]
c = [list(x) for x in zip(a, b)]
1
In [110]: A = [1,2,3]

In [111]: B = [4,5,6]

In [112]: list(zip(A,B))
Out[112]: [(1, 4), (2, 5), (3, 6)]
bigbounty
  • 14,834
  • 4
  • 27
  • 58