0

I have 2 questions regarding using nested loops.

  1. I am iterating over soccer_match to create a new list with the colors of each team. So far, I have
colors = []

for color in soccer_match:
    colors.append(color['colors'])

colors

Which gives me [['blue', 'white', 'red'], ['green', 'gold']]. How would I be able to combine the two lists into one? I haven't learned list comprehension or functions yet.

  1. How do I iterate over soccer_match to create a list with only the captains of each team? I'm assuming that I should use a nested for loop to look for 'captain' = True but not sure how to put that code.

Here is the given example. Thank you!

soccer_match = [
  { "home_team": True,
    "away_team": False,
    "country": "France",
    "num_passes": 484,
    "passes_completed": 423,
    "fouls_committed": 16,
    "colors": ["blue", "white", "red"],
    "players": [
      {
        "name": "Hugo LLORIS",
        "captain": True,
        "shirt_number": 1,
        "position": "Goalie"
      },
      {
        "name": "Benjamin PAVARD",
        "captain": False,
        "shirt_number": 2,
        "position": "Defender"
      },
      {
        "name": "Raphael VARANE",
        "captain": False,
        "shirt_number": 4,
        "position": "Defender"
      },
      {
        "name": "Samuel UMTITI",
        "captain": False,
        "shirt_number": 5,
        "position": "Defender"
      },
      {
        "name": "Paul POGBA",
        "captain": False,
        "shirt_number": 6,
        "position": "Midfield"
      },
      {
        "name": "Antoine GRIEZMANN",
        "captain": False,
        "shirt_number": 7,
        "position": "Forward"
      },
      {
        "name": "Kylian MBAPPE",
        "captain": False,
        "shirt_number": 10,
        "position": "Forward"
      },
      {
        "name": "Ousmane DEMBELE",
        "captain": False,
        "shirt_number": 11,
        "position": "Forward"
      },
      {
        "name": "Corentin TOLISSO",
        "captain": False,
        "shirt_number": 12,
        "position": "Midfield"
      },
      {
        "name": "Ngolo KANTE",
        "captain": False,
        "shirt_number": 13,
        "position": "Midfield"
      },
      {
        "name": "Lucas HERNANDEZ",
        "captain": False,
        "shirt_number": 21,
        "position": "Defender"
      }
    ],
  },
  { "home_team": False,
    "away_team": True,
    "country": "Australia",
    "num_passes": 390,
    "passes_completed": 332,
    "fouls_committed": 19,
    "colors": ["green", "gold"],
    "players": [
      {
        "name": "Mathew RYAN",
        "captain": False,
        "shirt_number": 1,
        "position": "Goalie"
      },
      {
        "name": "Mark MILLIGAN",
        "captain": False,
        "shirt_number": 5,
        "position": "Defender"
      },
      {
        "name": "Mathew LECKIE",
        "captain": False,
        "shirt_number": 7,
        "position": "Forward"
      },
      {
        "name": "Robbie KRUSE",
        "captain": False,
        "shirt_number": 10,
        "position": "Forward"
      },
      {
        "name": "Andrew NABBOUT",
        "captain": False,
        "shirt_number": 11,
        "position": "Forward"
      },
      {
        "name": "Aaron MOOY",
        "captain": False,
        "shirt_number": 13,
        "position": "Midfield"
      },
      {
        "name": "Mile JEDINAK",
        "captain": True,
        "shirt_number": 15,
        "position": "Midfield"
      },
      {
        "name": "Aziz BEHICH",
        "captain": False,
        "shirt_number": 16,
        "position": "Defender"
      },
      {
        "name": "Joshua RISDON",
        "captain": False,
        "shirt_number": 19,
        "position": "Defender"
      },
      {
        "name": "Trent SAINSBURY",
        "captain": False,
        "shirt_number": 20,
        "position": "Defender"
      },
      {
        "name": "Tom ROGIC",
        "captain": False,
        "shirt_number": 23,
        "position": "Midfield"
      }
    ]
  }
]
4
  • Please ask only one question per post. Please take the tour, read what's on-topic here, How to Ask, and the question checklist, and how to provide a minimal reproducible example. Welcome to Stack Overflow! Commented Oct 25, 2021 at 19:40
  • so you would like the the output to be ["red","blue","white","green","gold"] Commented Oct 25, 2021 at 19:40
  • The fastest way to flatten the list I recently discovered is flattened = sum(YOUR_LIST, []) Commented Oct 25, 2021 at 19:41
  • That about duplicates? Commented Oct 25, 2021 at 19:41

2 Answers 2

6

Replace colors.append with colors.extend.

Sign up to request clarification or add additional context in comments.

Comments

0

You should name your variables according to what they represent. In this case, each element of soccer_match represents a team.

Now for each team, team['colors'] is a list. For example, the first team has team['colors'] = ["blue", "white", "red"]. Since you want to add all elements of this list to your main colors list, use the lst.extend(x) method to add all elements to your current list lst from a given list x. lst.append(x) adds the entire list x as a single element to lst. See the documentation

To find captains, loop over all players in each team. If that player's 'captain' key is True, append the player to the captains list.

colors = []
captains = []

for team in soccer_match:
    colors.extend(team['colors'])
    for player in team['players']:
        if player['captain']:
            captains.append(player)

2 Comments

Thank you so much, I'll keep future questions to 1 per. I have a secondary question. when the loop is iterating through the team['players'] does it automatically figure that it shouldn't append any players with 'captain' = False?
@imkusoh re. " does it automatically figure that it shouldn't append any players with 'captain' = False": No! We asked it to do captains.append(player) only if player['captain'] is True, because that's how if statements work: the code inside that block will be executed only if the condition is true.

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.