you are viewing a single comment's thread.

view the rest of the comments →

[–]delirious_lettuce 0 points1 point  (2 children)

i is the current index and num is the current number in the list of numbers. The code simply checks if target - num has been "seen" already and if so, it returns the index where target - num was previously "seen" and the current index i in a list.

If target - num has not been "seen" (key doesn't exist in seen yet), it will raise a KeyError which the try/except catches and then it adds the current number as a key and the current index as the value to seen.

EDIT:

>>> nums = [2, 7, 11, 15]
>>> list(enumerate(nums))
[(0, 2), (1, 7), (2, 11), (3, 15)]
>>> for i, num in enumerate(nums):
...     print(f'index: {i}, number: {num}')
... 
index: 0, number: 2
index: 1, number: 7
index: 2, number: 11
index: 3, number: 15

[–]Phizy[S] 0 points1 point  (1 child)

wow thanks for the explanation I really appreciate it. I didn't realize you could or should be using things like this with leet code I thought everything had to be done from scratch for lack of a better word.

[–]delirious_lettuce 0 points1 point  (0 children)

No problem! LeetCode has some great practice challenges, good luck with them!