-1

I have a list of lists like the following:

l = [[[2.04356, 0.799842], 0.940545], [[0.600883, 0.363704], -0.104026], [[-0.150264, -0.0907573], -0.756651]]

I now want to split this list into two lists:

X = [[2.04356, 0.799842],[0.600883, 0.363704],[-0.150264, -0.0907573]]
y = [[0.940545],[-0.104026],[-0.756651]]

I thought either

my_list2, my_list1 = zip(*l)

or

my_list1 = [i[0] for i in l]
my_list2 = [i[1] for i in l]

would work but they don't give me the desired output.

Ron
  • 167
  • 6

2 Answers2

4

Your my_list1 is ok, but second should read i[1] and wrap in a list [i[1]] to get your output

x_list = [i[0] for i in l]
y_list = [[i[1]] for i in l]
azro
  • 53,056
  • 7
  • 34
  • 70
1

You can try numpy.

# create a nested list
my_list = [[[2.04356, 0.799842], 0.940545], [[0.600883, 0.363704], -0.104026], [[-0.150264, -0.0907573], -0.756651]]

# convert to numpy array
import numpy as np
arr_l = np.array(my_list)

my_list1 = arr_l[:, 0]
my_list2 = arr_l[:, 1]

This can be useful when your list goes very big.

Snoopy
  • 138
  • 6