title: How to avoid nested for loops when combining values from two lists of dictionaries in Python?
body: <p>Lets say I have two lists:</p>
  <pre><code>list1 = [{&quot;sport&quot;: 'Soccer',
            &quot;leagues&quot;: [{&quot;id&quot;: 1001,
                         &quot;name&quot;: &quot;League1&quot;,
                         &quot;events&quot;: [{&quot;id&quot;: 100,
                                     &quot;home&quot;: &quot;team1&quot;,
                                     &quot;away&quot;: &quot;team2&quot;},
                                    {&quot;id&quot;: 101,
                                     &quot;home&quot;: &quot;team3&quot;,
                                     &quot;away&quot;: &quot;team4&quot;}]}]},
           {&quot;sport&quot;: 'Basketball',
            &quot;leagues&quot;: [{&quot;id&quot;: 1002,
                         &quot;name&quot;: &quot;League2&quot;,
                         &quot;events&quot;: [{&quot;id&quot;: 200,
                                    &quot;home&quot;: &quot;team5&quot;,
                                    &quot;away&quot;: &quot;team6&quot;},
                                   {&quot;id&quot;: 201,
                                    &quot;home&quot;: &quot;team7&quot;,
                                    &quot;away&quot;: &quot;team8&quot;}]},
                        {&quot;id&quot;: 1003,
                         &quot;name&quot;: &quot;League3&quot;,
                         &quot;events&quot;: [{&quot;id&quot;: 300,
                                     &quot;home&quot;: &quot;team9&quot;,
                                     &quot;away&quot;: &quot;team10&quot;},
                                    {&quot;id&quot;: 301,
                                     &quot;home&quot;: &quot;team11&quot;,
                                     &quot;away&quot;: &quot;team12&quot;}]}],
        }
       ]
  
  list2 = [{&quot;sport&quot;: 'Soccer',
            &quot;leagues&quot;: [{&quot;id&quot;: 1001,
                         &quot;events&quot;: [{&quot;id&quot;: 100,
                                     &quot;odds&quot;: {&quot;home&quot;: 1.862, &quot;away&quot;: 1.847}},
                                    {&quot;id&quot;: 101,
                                     &quot;odds&quot;: {&quot;home&quot;: 1.70, &quot;away&quot;: 2.10}}]}]},
           {&quot;sport&quot;: 'Basketball',
            &quot;leagues&quot;: [{&quot;id&quot;: 1002,
                         &quot;events&quot;: [{&quot;id&quot;: 200,
                                     &quot;odds&quot;: {&quot;home&quot;: 1.952, &quot;away&quot;: 1.952}},
                                    {&quot;id&quot;: 201,
                                     &quot;odds&quot;: {&quot;home&quot;: 1.90, &quot;away&quot;: 2.05}}]},
                        {&quot;id&quot;: 1003,
                         &quot;events&quot;: [{&quot;id&quot;: 300,
                                     &quot;odds&quot;: {&quot;home&quot;: 1.5, &quot;away&quot;: 2.7}},
                                    {&quot;id&quot;: 301,
                                     &quot;odds&quot;: {&quot;home&quot;: 1.75, &quot;away&quot;: 2.09}}]}]}]
  </code></pre>
  <p>I would like to combine events from list1 with their respective &quot;odds&quot; from list2.
  In reality there are many more elements in these two lists, the example is simplified for the sake of clarity.
  My current (ugly) solution:</p>
  <pre><code>for sport_list1 in list1:
      for sport_list2 in list2:
          if sport_list1['sport'] == sport_list2['sport']:
              for league_list1 in sport_list1['leagues']:
                  for league_list2 in sport_list2['leagues']:
                      if league_list1['id'] == league_list2['id']:
                          for event_list1 in league_list1['events']:
                              for event_list2 in league_list2['events']:
                                  if event_list1['id'] == event_list2['id']:
                                      print(sport_list1['sport'], league_list1['name'], event_list1['home'], event_list1['away'], event_list2['odds'])
                                      break
  </code></pre>
  <p>Desired output:</p>
  <pre><code>Soccer League1 team1 team2 {'home': 1.862, 'away': 1.847}
  Soccer League1 team3 team4 {'home': 1.7, 'away': 2.1}
  Basketball League2 team5 team6 {'home': 1.952, 'away': 1.952}
  Basketball League2 team7 team8 {'home': 1.9, 'away': 2.05}
  Basketball League3 team9 team10 {'home': 1.5, 'away': 2.7}
  Basketball League3 team11 team12 {'home': 1.75, 'away': 2.09}
  </code></pre>
  <p>Any way to make this cleaner and/or more efficient?</p>