-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]]
Red
  • 26,798
  • 7
  • 36
  • 58

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
  • 1,014
  • 1
  • 8
  • 14
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
  • 26,853
  • 21
  • 75
  • 114
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
  • 16,526
  • 5
  • 37
  • 65