I have a list of numbers:
nums = [-3,-2,-1,1,2,3,0,0]
And I want to iterate over the list, and add two numbers at a time
, and if the sum
of these two numbers is an element in the list (excluding the two we just summed), then I want to remove the element
. How can I do this?
For example, since -3+2= -1
and -1 is in the list
, I can remove -1 from the list.
I tried to create an empty list/set
to store the sums of each pair of elements, then compare the elements in nums to this list/set
, but this doesn't work, as it violates the second part of what I want to do- in brackets above
!
P.S. I would like to remove as many elements as possible
, and one worry is that if I remove elements as I iterate the list- before finding all of the pair sums- then I might miss some valid pair sums!
Any help is much appreciated.