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]]
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]]
Just use the following line. This should work.
xtotal = [list(t) for t in zip(x1, x2)]
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]]