Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through list of lists of lists

I am trying to iterate through a 3-D list in python(not numpy but I am willing to convert to a numpy array if this makes it easier) in such a way that from a list like this:

a = [[[0, 0], [3, 0]], [[1, 0], [4, 0]], [[2, 0], [6, 0]] ]

I can get the output

[0,0]
[1,0]
[2,0]
[3,0]
[4,0]
[6,0]

I can't figure out how to make it iterate like this...

My code:

a = [[[0, 0], [0, 0]], [[1, 0], [0, 0]], [[2, 0], [0, 0]] ]
for i in range(len(a)):
    for z in range(len(a[i])):
        print(a[i][z])

I've tried different things but can't seem to get this output.

like image 998
devman3211 Avatar asked Dec 06 '25 10:12

devman3211


1 Answers

I think you want to print the nth sub-sublists consecutively from each sublist. You could unpack and zip a to get an iterable of tuples, then print each pair in them:

for tpl in zip(*a):
    for pair in tpl:
        print(pair)
        

Output:

[0, 0]
[1, 0]
[2, 0]
[3, 0]
[4, 0]
[6, 0]

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!