0

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": "Gazi\u015fehir 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']
1
  • As this question has already been answered, I think it's also important to note that you don't have to iterate through the JSON every time, you could simply put all of those .append statements into the same loop and it will give you the same output. Commented Jan 30, 2021 at 21:09

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

This creates a list of strings in the format that you want. As a bonus, python is quite nice for iterables so it can be done in one line.

list_of_all = [f"{item['time']['elapsed']}' {item['player']['name']} ({item['assist']['name']}) {item['detail']}" for item in data['response']]

Comments

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.