imkusoh
imkusoh

Reputation: 43

using nested loops - for loop

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"
      }
    ]
  }
]

Upvotes: 0

Views: 941

Answers (2)

pho
pho

Reputation: 25489

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)

Upvotes: 0

Frank Yellin
Frank Yellin

Reputation: 11230

Replace colors.append with colors.extend.

Upvotes: 6

Related Questions