Dev Helpers
← Back

Find two-sum indices

Find two-sum indices. A self-contained example using the Python standard library.

Open this example in the interactive library

Source code

def two_sum(values, target):
    seen = {}
    for i, value in enumerate(values):
        if target-value in seen:
            return seen[target-value], i
        seen[value] = i
    return None
print(two_sum([2, 7, 11], 9))

Sample output

(0, 1)