title: How to create a list of N items with a budget constraint and multiple conditions on Python
body: <p>I have the following df of Premier League players (ROI_top_players):</p>
  <pre><code>    player                 team         position    cost_2223   total_points  ROI   
  0   Mohamed Salah          Liverpool    FWD         13.0        259           29.77 
  1   Trent Alexander        Liverpool    DEF         8.4         206           24.52 
  2   Jarrod Bowen           West Ham     MID         8.5         204           23.56
  3   Kevin De Bruyne        Man  City    MID         12.0        190           15.70
  4   Virgil van Dijk        Liverpool    DEF         6.5         183           14.91 
  ... ... ... ... ... ... ... ... ... ...
  151 Jamaal Lascelles       Newcastle    DEF         4.5         45            10.22
  152 Ben Godfrey            Everton      GKP         4.5         45            9.57  
  153 Aaron Wan-Bissaka      Man Utd      DEF         4.5         41            8.03
  154 Brandon Williams       Norwich      DEF         4.0         36            7.23  
  </code></pre>
  <p>I want to create a list of 15 players (must be 15 - not more, not less), with the highest ROI possible, and it has to fulfill certain conditions:</p>
  <ul>
  <li><strong>Position constraints</strong>: it must have 2 GKP, 5 DEF, 5 MID, and 3 FWD</li>
  <li><strong>Budget constraint</strong>: I have a budget of $100, so for each player I add to the list, I must subtract the player's cost (cost_2223) from the budget.</li>
  <li><strong>Team constraint</strong>: It can't have more than 3 players per club.</li>
  </ul>
  <p>Here's my current code:</p>
  <pre><code>def get_ideal_team_ROI(budget = 100, star_player_limit = 3, gk = 2, df = 5, md = 5, fwd = 3):
      money_team = []
      budget = budget
      positions = {'GK': gk, 'DEF': df, 'MID': md, 'FWD': fwd}
      for index, row in ROI_top_players.iterrows():
         if (budget &gt;= row['cost_2223'] and positions[row['position']] &gt; 0):
             money_team.append(row['player'])
             budget -= row['cost_2223']
             positions[row['position']] = positions[row['position']] - 1
      return money_team
  </code></pre>
  <p>This code has two problems:</p>
  <ol>
  <li>It creates the list BUT, the list does not end up with 15 players.</li>
  <li>It doesn't fulfill the team constraint (I have more than 3 players per team).</li>
  </ol>
  <p>How should I tackle this? I want my code to make sure that I always have enough budget to buy 15 players and that I always have at maximum 3 players per team.</p>
  <p>**I do not need all possible combinations. Just ONE team with the highest possible ROI.</p>