Finding two numbers that add to a given total Given there's a list of random number and the amount I want to find.

For example:


  [80, 98, 83, 92, 1, 38, 37, 54, 58, 89]


And I want two numbers that add up to a given total in this case it's 181. So, it's going to be (92+89). The list could be really big. 

Here's the solution I come up with. But it's kinds brute force and I was wondering if there's a better way of doing this.

for i, item in enumerate(numbers):
    for j in range(i+1, len(numbers)):
        total_of_two_items = numbers[i] + numbers[j]
        if(total_of_two_items == total_number):
            print '{first_item} {second_item}'.format(first_item=i+1, second_item=j+1)
            print '\n'
Here is an alternative approach using set logic, which is O(n) in the average case:

n = 181
n2 = n//2
numbers = [80, 98, 83, 92, 1, 38, 37, 54, 58, 89]
goodnums = {n-x for x in numbers if x&lt;=n2} &amp; {x for x in numbers if x&gt;n2}
pairs = {(n-x, x) for x in goodnums}


What this does is first filter out values that are greater than 1/2 the target value, since one number in each pair must be that way.  Then it subtracts the remaining numbers from the target (in this case 181).  This gets the other value from the pair.  Then it uses set logic to extract only those values where the other value is present in the original list of numbers.

So to put it more briefly, it finds all values x such that 181-x is also present in the list.

Edit: If you don't want to include cases where both members of the pair are equal and it only exists once, such as n=2 and numbers = [1], as Gareth pointed out, add this to the end:

if not n%2 and (n2, n2) in pairs and numbers.count(n2) == 1:
   pairs.remove((n2, n2))


This will check if n is even and, if so, if there is exactly one value where x==n//2, if so remove (n//2, n//2) from the results.