1

Sometimes, before optimizing your code, you may end up to have very slow running code in list comprehension statement. Because you have not yet tested the code fully, it easy to loose patience and just interrupt the code.

If you are making function, you can insert print statements for verification. But how to put print in list comprehension. If you are using function to produce the result you can insert a print there or you could use some debugging tools. I however often keep it simple and I came up with this pass through trivial function which basically just prints it's arguments and passes them through as result. You can insert this function for part which expects a sequence to iterate or produces a sublist or tuple.

Edited by pyTony: n/a

Votes + Comments
nice
def echo(*args):
    print(args)
    return(args)


n = 1000000
limit = int((n**2/2)**0.5) +1
result = [echo(x,(n**2 - x**2)**0.5) for x in range(int(n**0.5)-1,limit) if ((n**2 - x**2)**0.5).is_integer()]

Specialties:
IT/Science/Contracts/Religious translation/interpreting FIN-ENG-FIN
Python programming

2
Contributors
1
Reply
5
Views
4 Years
Discussion Span
Last Post by Gribouillis
2

It is nice, but I think echo() should take a single argument and return a single argument like this

def echo(arg):
    print(arg)
    return arg

Having a unary function allows use with imap() for example

from itertools import imap
s = sum(imap(echo, (x * x for x in range(5))))

Edited by Gribouillis: n/a

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.