Multiple print functions in list comprehension
<p>The goal of this post is to put multiple print functions throughout a list comprehension to visually understand what's happening within. </p>

<p>Important notes:</p>

<ul>
<li>This <strong>should not</strong> be used for anything other than educational purposes and trying to understand code. </li>
<li>If you are using Python 2.x, you need to add a future import (it's in the code I pasted) or else print won't work. Only functions work in list comprehension. Print in 2.x does not operate as a function. Or...just switch to Python 3.x. </li>
</ul>

<p>This was the original question:</p>

<pre><code>    ## Using future to switch Print to a function
    from __future__ import print_function 

    reg = []
    for x in [1,2,3]:
        for y in [3,1,4]:
            print('looping through',x,'then',y)
            if x == y:
                print('success',x,y)
                reg.append((x,y))
    print(reg)
</code></pre>

<p>Here's the equivalent list comprehension with no print statements.</p>

<pre><code>    from __future__ import print_function 
    comp = [(x,y) for x in [1,2,3] for y in [3,1,4] if x == y] 
    print(comp)
</code></pre>

<p>So is there any way to put in a bunch of print statements so both code print the same things?</p>

<hr>

<h3>Edit with solution to original question:</h3>

<p>Using the methods in the comments - I've figured it out!</p>

<p>So say you want to convert this.</p>

<pre><code>    from __future__ import print_function 

    x = 1
    y = 2
    z = 1
    n = 2

    [[a,b,c] for a in range(x+1) for b in range(y+1) for c in range(z+1) if a + b + c != n]
</code></pre>

<p>Adding print statements to print each loop, showing if it failed or not.</p>

<pre><code>    from __future__ import print_function 

    x = 1
    y = 2
    z = 1
    n = 2

    [
        [a,b,c] for a in range(x+1) for b in range(y+1) for c in range(z+1) if 
        (print('current loop is',a,b,c) or a + b + c != n)
        and
        (print('condition true at',a,b,c) or True)
    ]
</code></pre>

<p>So really the only thing that was changed was the conditional at the end.</p>

<pre><code>    (a + b + c != n) 
</code></pre>

<p>to</p>

<pre><code>    (print('current loop is',a,b,c) or a + b + c != n)
    and
    (print('condition true at',a,b,c) or True)
</code></pre>

<hr>

<h3>Additional Information:</h3>

<p>So there's good stuff in the comment section that I think would help others as well. I'm a visual learner so this website was great.</p>

<ul>
<li><a href="http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/#colored-comprehension" rel="nofollow">http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/#colored-comprehension</a></li>
</ul>

<p>(credits to Tadhg McDonald-Jensen)</p>

<p>List comprehension was introduced with <a href="https://www.python.org/dev/peps/pep-0202/" rel="nofollow">PEP 202</a> which states:</p>

<blockquote>
  <p>It is proposed to allow conditional construction of list literals
  <strong>using for and if clauses.</strong>  They would nest in the same way for loops
  and if statements nest now.</p>
</blockquote>

<p>List comprehension was designed to replace constructs that formed a list using only <code>for</code> loops, <code>if</code> conditionals and <code>.append</code> method once per iteration.  Any additional structure is not possible in list comprehensions so unless you stuck your prints into one of the allowed components you cannot add them.</p>

<p>That being said, putting a print statement in the conditional - while technically possible - is highly <strong>not</strong> recommended.</p>

<pre><code>[a for a in x if print("this is a bad way to test",a)]
</code></pre>

python python-2.7 python-3.x for-loop list-comprehension