0

This is a simple test function that is synonymous to my actual code. So I have an array of data structured like this,

a=[[1,2,3,4,5],[0,3,6,8,10],[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]]

I am trying to loop over it so that in each pass we get, the first element of the first list, first element in the second list, and then each element in the first list of the third list.

Like this.

for i,j in enumerate(a):
     print 'iterations',i
     print a[0][i]
     print a[1][i]
     print a[2][i][0]
     print a[2][i][1]
     print a[2][i][2]
     print a[2][i][3]

But for some reason I cant get passed i=2. Can anyone explain why and how I can rectify this. Thanks. There's no error it just ends at i=2.

2
  • yes it ends at 2 because u have list of three list at first level Commented Jun 2, 2014 at 13:02
  • 2
    Simple: len(a) == 3 in your example. Commented Jun 2, 2014 at 13:02

1 Answer 1

3

Look closely at structure of your data:

a = [
        [1,2,3,4,5],
        [0,3,6,8,10],
        [ 
            [1,2,3,4],
            [1,2,3,4],
            [1,2,3,4],
            [1,2,3,4],
            [1,2,3,4]
        ]
    ]

You clearly have only 3 elements in a, so 2 is the last index...

Sign up to request clarification or add additional context in comments.

1 Comment

Note that pprint.pprint can be used to spot this kind of error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.