jhub1
jhub1

Reputation: 631

Multiple print functions in list comprehension

The goal of this post is to put multiple print functions throughout a list comprehension to visually understand what's happening within.

Important notes:

This was the original question:

    ## 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)

Here's the equivalent list comprehension with no print statements.

    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)

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


Edit with solution to original question:

Using the methods in the comments - I've figured it out!

So say you want to convert this.

    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]

Adding print statements to print each loop, showing if it failed or not.

    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)
    ]

So really the only thing that was changed was the conditional at the end.

    (a + b + c != n) 

to

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

Additional Information:

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.

(credits to Tadhg McDonald-Jensen)

Upvotes: 2

Views: 1676

Answers (3)

Mark Ransom
Mark Ransom

Reputation: 308520

You need to evaluate your print function, but the return value isn't useful since it's always None. You can use and/or to combine it with another expression.

comp = [(x,y) for x in [1,2,3] for y in [3,1,4] if (print('looping through',x,'then',y) or x == y) and (print('success', x, y) or True)]

I really hope you're only doing this for educational purposes, because it's ugly as heck. Just because you can do something doesn't mean you should.

Upvotes: 2

Tadhg McDonald-Jensen
Tadhg McDonald-Jensen

Reputation: 21474

List comprehension was introduced with PEP 202 which states:

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

List comprehension was designed to replace constructs that formed a list using only for loops, if conditionals and .append 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.

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

[a for a in x if print("this is a bad way to test",a)]

Upvotes: 1

BPL
BPL

Reputation: 9863

I think you shouldn't running debug code inside list comprehensions, that said, if you wanted to do so, you could wrap your code inside a function like this:

from __future__ import print_function


def foo(x, y):
    print('looping through', x, 'then', y)
    if x == y:
        print('success', x, y)
        return (x, y)

comp = [foo(x, y) for x in [1, 2, 3] for y in [3, 1, 4] if x == y]
print(comp)

Upvotes: 3

Related Questions