List/Conditional Comprehension in Python
<p>I was wondering how to make the following into a comprehension:</p>

<pre><code>for g in a:
    for f in g[::3]:
        if f == clf.best_params_:
            print g
</code></pre>

<p>I tried this:</p>

<pre><code>p = [[print g for g in a] if f==clf.best_params for f in [g[::3] for g in a]]
p
</code></pre>

<p>but got an error at <code>for f in</code> </p>

<p>Would love some help! Thanks!</p>

<p>The correct way to translate those loops would be</p>

<pre><code>[print g for g in a for f in g[::3] if f == clf.best_params_]
</code></pre>

<p>However, in Python 2 <code>print g</code> is a statement, and that's why you get a SyntaxError. The Python 3 counterpart (i.e. with <code>print(g)</code>) would work, but note that a list comprehension here is completely useless. A list comprehension is useful when you have to gather values into a list - here it just hampers the readability.</p>

<p>Regarding the SyntaxError, it's not the primary issue here. Note that you can use</p>

<pre><code>from __future__ import print_function
</code></pre>

<p>and then use the <code>print()</code> function in your list comprehension as you would in Python 3.</p>

python list scikit-learn conditional list-comprehension