I have a 3d list
aa = [[[2, 3, 4, 5],
[ 6, 7, 8, 9]], [[11, 12, 14, 15]]], which consists of two 2d lists
how do I get this result
[[2, 6], [11]]
the first element of each sub list.
b = []
for i, row in enumerate(aa):
for j, rr in enumerate(row):
b.append(rr[0])
gives
[2,6,11]
CodePudding user response:
You can do it with a list comprehension:
[[i[0] for i in j] for j in aa]
Output:
[[2, 6], [11]]
CodePudding user response:
Just store each first element in a sublist and merge those sublists to get your result
arr here is your input array
ret = []
for twoD in arr:
temp=[]
for oneD in twoD:
temp.append(oneD[0])
ret.append(temp)
print(ret)
Output
[[2, 6], [11]]