gdogg371
gdogg371

Reputation: 4122

For loops and nested lists issue

I have a set of nested list being returned via the JSON.Loads method from a website XHR request:

[[[13, u'Arsenal', [[[[0, 1], [1, 18], [7, 1], [8, 1], [[[u'fk_foul_lost', [82]], 
[u'total_red_card', [0]], [u'total_yel_card', [21]]]]]]]], 
    ...
    ...
    ...
[184, u'Burnley', [[[[1, 11], [9, 1], [[[u'fk_foul_lost', [78]], [u'total_red_card', [0]], 
[u'total_yel_card', [12]]]]]]]], [259, u'Swansea', [[[[0, 3], [1, 14], [[[u'fk_foul_lost', [99]], 
[u'total_red_card', [2]], [u'total_yel_card', [13]]]]]]]]]]

Where the above nested list is allocated to the variable responserI am using the following code:

for match in responser: 
    for num_events, team, events in match:

        for y in events[0]:
            for sub in y:
            print sub

This returns a result like this:

[0, 1]
[1, 18]
[7, 1]
[8, 1]
[[[u'fk_foul_lost', [82]], [u'total_red_card', [0]], [u'total_yel_card', [21]]]]
...
...
...
[1, 11]
[9, 1]
[[[u'fk_foul_lost', [78]], [u'total_red_card', [0]], [u'total_yel_card', [12]]]]
[0, 3]
[1, 14]
[[[u'fk_foul_lost', [99]], [u'total_red_card', [2]], [u'total_yel_card', [13]]]]

However what I want is just the numeric values within:

[[[u'fk_foul_lost', [99]], [u'total_red_card', [2]], [u'total_yel_card', [13]]]]

Can anyone tell me the syntax I need to finish this code off?

Thanks

Upvotes: 1

Views: 46

Answers (1)

friedi
friedi

Reputation: 4360

Try this:

for match in responser: 
    for num_events, team, events in match:

        for y in events[0]:
            for sub in y:
                if isinstance(sub[0], list):
                    print sub

Upvotes: 2

Related Questions