-1

I am trying to create a nested list from two lists

x1 = [1, 2]
x2 = [Z, Y]

The output should be like this:

xtotal = [[1, Z], [2, Y]]

My code:

xtotal = [[x for x in x1], [xx for xx in x2]]
Casey Nwan
  • 13
  • 3

2 Answers2

2

Just use the following line. This should work.

xtotal = [list(t) for t in zip(x1, x2)]
Md Golam Rahman Tushar
  • 2,175
  • 1
  • 14
  • 29
0

Assuming that x1 and x2 have the same length then

Code:

x1 = [1, 2]
x2 = [Z, Y]
xtotal = [[x1[i], x2[i]] for i in range(len(x1))]

Output:

[[1, Z], [2, Y]]
Abdo Sabry
  • 82
  • 7