A list takes you from a place to a value: values[3]. Sometimes you need the reverse: "where was the value 7?" Write the value as the key and the place as the value in a dictionary and you answer that in one step.
seen = {}for i, v in enumerate(values):if key - v in seen:return [seen[key - v], i]seen[v] = ireturn []
key, each element has exactly one possible partner: key - v. If you have seen that partner, take its place from the dictionary. Ask first, then write: that way an element never pairs with itself.This pattern ("have I seen what I am looking for, and if so where") is the skeleton of a large share of interview questions.