Skip to content
Advertisement

Combine Multiple Lists (Python)

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:

elapsed = []
player = []
assist = []
detail = []

for item in data['response']:
        player.append(item['player']['name'])

for item in data['response']:
        elapsed.append(item['time']['elapsed'])

for item in data['response']:
        assist.append(item['assist']['name'])

for item in data['response']:
        detail.append(item['detail'])

JSON file:

{
  "get": "fixtures/events",
  "parameters": { "fixture": "599120", "type": "goal" },
  "errors": [],
  "results": 3,
  "paging": { "current": 1, "total": 1 },
  "response": [
    {
      "time": { "elapsed": 51, "extra": null },
      "team": {
        "id": 645,
        "name": "Galatasaray",
        "logo": "https://media.api-sports.io/football/teams/645.png"
      },
      "player": { "id": 456, "name": "H. Onyekuru" },
      "assist": { "id": 19034, "name": "R. Babel" },
      "type": "Goal",
      "detail": "Normal Goal",
      "comments": null
    },
    {
      "time": { "elapsed": 79, "extra": null },
      "team": {
        "id": 645,
        "name": "Galatasaray",
        "logo": "https://media.api-sports.io/football/teams/645.png"
      },
      "player": { "id": 456, "name": "H. Onyekuru" },
      "assist": { "id": 142959, "name": "K. Akturkoglu" },
      "type": "Goal",
      "detail": "Normal Goal",
      "comments": null
    },
    {
      "time": { "elapsed": 90, "extra": 7 },
      "team": {
        "id": 3573,
        "name": "Gaziu015fehir Gaziantep",
        "logo": "https://media.api-sports.io/football/teams/3573.png"
      },
      "player": { "id": 25921, "name": "A. Maxim" },
      "assist": { "id": null, "name": null },
      "type": "Goal",
      "detail": "Penalty",
      "comments": null
    }
  ]
}

Output:

['H. Onyekuru', 'H. Onyekuru', 'A. Maxim']
[51, 79, 90]
['R. Babel', 'K. Akturkoglu', None]
['Normal Goal', 'Normal Goal', 'Penalty']

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.

data = {
  # ... elided for brevity, see OP's post
}
for event in data["response"]:
  print(f"{event['time']['elapsed']}' {event['player']['name']} (A: {event['assist']['name']}) {event['detail']}")

This prints out

51' H. Onyekuru (A: R. Babel) Normal Goal
79' H. Onyekuru (A: K. Akturkoglu) Normal Goal
90' A. Maxim (A: None) Penalty
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement