I am trying to combine multiple lists in selected format. Simply, trying to create
elapsed + "' " + player + ' (A: ' + assist + ') - ' + detail
(for example: 51' H. Onyekuru (A: R. Babel) - Normal Goal
). I also added the json file i took the data. Maybe it can be created directly without creating lists.
Code:
JavaScript
x
17
17
1
elapsed = []
2
player = []
3
assist = []
4
detail = []
5
6
for item in data['response']:
7
player.append(item['player']['name'])
8
9
for item in data['response']:
10
elapsed.append(item['time']['elapsed'])
11
12
for item in data['response']:
13
assist.append(item['assist']['name'])
14
15
for item in data['response']:
16
detail.append(item['detail'])
17
JSON file:
JavaScript
1
49
49
1
{
2
"get": "fixtures/events",
3
"parameters": { "fixture": "599120", "type": "goal" },
4
"errors": [],
5
"results": 3,
6
"paging": { "current": 1, "total": 1 },
7
"response": [
8
{
9
"time": { "elapsed": 51, "extra": null },
10
"team": {
11
"id": 645,
12
"name": "Galatasaray",
13
"logo": "https://media.api-sports.io/football/teams/645.png"
14
},
15
"player": { "id": 456, "name": "H. Onyekuru" },
16
"assist": { "id": 19034, "name": "R. Babel" },
17
"type": "Goal",
18
"detail": "Normal Goal",
19
"comments": null
20
},
21
{
22
"time": { "elapsed": 79, "extra": null },
23
"team": {
24
"id": 645,
25
"name": "Galatasaray",
26
"logo": "https://media.api-sports.io/football/teams/645.png"
27
},
28
"player": { "id": 456, "name": "H. Onyekuru" },
29
"assist": { "id": 142959, "name": "K. Akturkoglu" },
30
"type": "Goal",
31
"detail": "Normal Goal",
32
"comments": null
33
},
34
{
35
"time": { "elapsed": 90, "extra": 7 },
36
"team": {
37
"id": 3573,
38
"name": "Gaziu015fehir Gaziantep",
39
"logo": "https://media.api-sports.io/football/teams/3573.png"
40
},
41
"player": { "id": 25921, "name": "A. Maxim" },
42
"assist": { "id": null, "name": null },
43
"type": "Goal",
44
"detail": "Penalty",
45
"comments": null
46
}
47
]
48
}
49
Output:
JavaScript
1
5
1
['H. Onyekuru', 'H. Onyekuru', 'A. Maxim']
2
[51, 79, 90]
3
['R. Babel', 'K. Akturkoglu', None]
4
['Normal Goal', 'Normal Goal', 'Penalty']
5
Advertisement
Answer
Sure you can – just iterate over the events and print out those lines (or gather them into a list if you like, for example). The f-string syntax below requires Python 3.6 or newer.
JavaScript
1
6
1
data = {
2
# ... elided for brevity, see OP's post
3
}
4
for event in data["response"]:
5
print(f"{event['time']['elapsed']}' {event['player']['name']} (A: {event['assist']['name']}) {event['detail']}")
6
This prints out
JavaScript
1
4
1
51' H. Onyekuru (A: R. Babel) Normal Goal
2
79' H. Onyekuru (A: K. Akturkoglu) Normal Goal
3
90' A. Maxim (A: None) Penalty
4