Search code examples
pythonlist3d2d

python 3d list manipulation


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]


Solution

  • You can do it with a list comprehension:

    [[i[0] for i in j] for j in aa]
    

    Output:

    [[2, 6], [11]]