user21575: Say that I currently have a board, and then on that board I move my e2 pawn to e4. I'm looking for a way to get the engine to predict the X amount of best moves for black.
I know how to predict the best move given a position:
result = engine.play(board, chess.engine.Limit(time=0.1))
board.push(result.move)

But that only predicts the number 1 best move. I want a list containing the second, third ... X best moves.
How would I do that?

user15989: In the world of chess engines, what you're looking for is called multi-PV (multiple principal variations): telling the engine to look for and return up to n best moves and the associated principal variations for each. ("Up to" because sometimes there's only 1 legal move in a position.)
In python-chess, this is part of the engine protocols, as one of the optional parameters when calling engine.analyse(). From the documentation:

class chess.engine.EngineProtocol
coroutine analyse( [...] )

multipv – Optional. Analyse multiple root moves. Will return a list of at most multipv dictionaries rather than just a single info dictionary.


The .analyse() method tells the engine to analyse the position, returning a dictionary of information, which includes among other things the moves of the principal variation. Specifying for instance .analyse(multipv = 3) will tell the engine to return a list of (up to) 3 such dictionaries -- the first moves in the pv value of each dictionary should be what you are looking for.

21575: <p>Say that I currently have a board, and then on that board I move my e2 pawn to e4. I'm looking for a way to get the engine to predict the X amount of best moves for black.</p>

<p>I know how to predict the best move given a position:</p>

<pre><code>result = engine.play(board, chess.engine.Limit(time=0.1))
board.push(result.move)
</code></pre>

<p>But that only predicts the number 1 best move. I want a list containing the second, third ... X best moves.</p>

<p>How would I do that?</p>
 15989: <p>In the world of chess engines, what you're looking for is called <strong>multi-PV</strong> (multiple principal variations): telling the engine to look for and return up to <em>n</em> best moves and the associated principal variations for each. ("Up to" because sometimes there's only 1 legal move in a position.)</p>

<p>In python-chess, this is part of the <a href="https://python-chess.readthedocs.io/en/latest/engine.html" rel="nofollow noreferrer">engine protocols</a>, as one of the optional parameters when calling <code>engine.analyse()</code>. From the documentation:</p>

<blockquote>
  <p><code>class chess.engine.EngineProtocol</code></p>
  
  <p><code>coroutine analyse( [...] )</code></p>
  
  <ul>
  <li>multipv – Optional. Analyse multiple root moves. Will return a list of at most multipv dictionaries rather than just a single info dictionary.</li>
  </ul>
</blockquote>

<p>The <code>.analyse()</code> method tells the engine to analyse the position, returning a <a href="https://python-chess.readthedocs.io/en/latest/engine.html#chess.engine.PlayResult.info" rel="nofollow noreferrer">dictionary of information</a>, which includes among other things the moves of the principal variation. Specifying for instance <code>.analyse(multipv = 3)</code> will tell the engine to return a list of (up to) 3 such dictionaries -- the first moves in the <code>pv</code> value of each dictionary should be what you are looking for.</p>
