[Tutor] Trying to find all pairs of numbers from list whose sum is 10

Manprit Singh manpritsinghece at gmail.com
Sat Jun 19 14:24:19 EDT 2021


Dear sir,

Let us consider a list
lst = [2, 4, 7, 5, 9, 0, 8, 5, 3, 8]
in this list there are 3 pairs (2, 8), (5, 5), (7, 3) whose sum is 10. To
finding these pairs i have written the code as below :

def pair_ten(x):
    y, st  = x[:], set()
    for i in x:
        y.remove(i)
        st.update((i, ele) for ele in y if i+ele == 10)
    return st



lst = [2, 4, 7, 5, 9, 0, 8, 5, 3, 8]
ans = pair_ten(lst)
print(ans)

{(2, 8), (5, 5), (7, 3)}

But i feel the code is still more complex, due to two for loops, in
what way this ccan be dome more efficiently.

kindly guide

Regards

Manprit singh


More information about the Tutor mailing list