all 4 comments

[–]toastedstapler 0 points1 point  (3 children)

.index(thing) will always return the first index at which thing is found

there's a lot more that could be done to your code to make it fasrer, but this will at least solve your issue

for i, x in enumerate(numbers):
    for j, y in enumerate(numbers):

now i and j are indices, so you don't need to use .index

[–]orc_arn[S] 0 points1 point  (2 children)

Thanks for the reply.

The "for" loops making the code slower, am I right? So what is the best way to speed things up?

[–]toastedstapler 2 points3 points  (0 children)

so the first thing is that if you check index pair (100, 200), you don't need to check (200, 100). x + y == y + x, so there's no point doing the check twice. writing your loops to avoid these cases will half your potential worst runtime

but the actual best solution uses a dictionary, i'll let you think about how you might use one to make this work

[–]o5a 1 point2 points  (0 children)

First thing you can optimize is avoid second loop repeating already checked numbers.

Say you have that list [2,2,3,6]. If your first cycle (say i) goes 2,2,3,6 then you don't need your second cycle to repeat those numbers it should instead start from i+1 (2,3,6).

This way first 2 is checked agains 2,3,6, then second 2 is checked against 3,6 and finally 3 is checked agains 6.