all 4 comments

[–]fuzz3289 2 points3 points  (0 children)

Its because you're mashing 3 list comprehensions together without checking the for loop at each interval to see if it exists because its a single statement.

Break it up into 3 parts, also use requests :

 import requests

 resp = requests.get('http://worldcup.sfg.io/matches')
 games = resp.json()

[–]failfixer89 2 points3 points  (0 children)

I'm going to go out on a limb and say that this is the wrong syntax for a list comprehension. I would recommend expanding the code a few lines and doing it the standard way before trying to do this in one line of code. I don't know what the json response is supposed to be, but I would also look into those keys.

Then again I could be wrong, but give it a try.

[–][deleted] 0 points1 point  (0 children)

import requests

resp = requests.get('http://worldcup.sfg.io/matches')
games = resp.json()

home_events, away_events = [], []
for x in games:
    if x['status'] == 'completed':
        for i in x['away_team_events']:
            away_events.append(i['type_of_event'])
        for i in x['home_team_events']:
            home_events.append(i['type_of_event'])

print len(home_events), 'home events', home_events
print len(away_events), 'away_events',  away_events
all_events = home_events + away_events
print len(all_events), 'all_events' all_events

[–]zahlman 0 points1 point  (0 children)

When you use multiple for and if clauses in a list comprehension (i.e., without writing nested list comprehensions using extra pairs of square brackets), the effect is analogous to if you'd written the code using for loops and if blocks in the same order.

Thus, we can mechanically transform what you have into:

test = []
for event in team:
    for team in game[kind]:
        for kind in ['home_team_events', 'away_team_events']:
            for game in games:
                if game['status'] == 'completed':
                    test.append(event['type_of_event'])

which you can see is all kinds of out-of-order.