Python: How do I compare one element of a list with every other elements besides itself?

Here’s a readable iteration approach. Compute max among all indices except the target index:

listA = [1,2,3,4,5]

TARGET_INDEX = 4
maximum = 0
for i, val in enumerate(listA):
    if i == TARGET_INDEX:
        continue

    maximum = max(maximum, val*listA[TARGET_INDEX])

print maximum

Leave a Comment