Checking the sum of pairs to match a target value I'm new to Python and curious what looks good and what doesn't. The function I have defined looks for pairs in a list of integers and tells the user whether or not any possible pair equals the target number. How can this code be more efficient or fast?

import datetime
from itertools import combinations

def csop(*params):
    t1 = datetime.datetime.now()
    print params[0], params[1]
    for c in combinations(params[0],2):
        print sum(c)
        if sum(c) == params[1]:
            print 'Target number reached'
            t2 = datetime.datetime.now()
            print t2-t1
            exit()
        else:
            pass


To run:

import csop
csop.csop([1,2,3,4],5)


The output should look something like this:



[1, 2, 3, 4] 5
3
4
5
Target number reached
0:00:00.000071
As a style note, this is a bit of an abuse:

def csop(*params):


The point of *params is to allow things to be passed that cannot be known when the code is written. This is not the case here.  There are only two parameters, and you know what they are.  So you should give them names.

This:

exit()


is also bad form.  Exiting the program from the middle of a function is great way to make bugs that are really hard to find.  In this case simply return

this:

else:
    pass


is completely dead code.  pass is used to mark a block that is empty.  It is a by product of everything in Python being indent sensitive.  But the else is optional so just leave it and the pass off.

And performance wise, you perform the sum() twice.  If it matters to you, this result could be saved instead of evaluating it twice.