I have a question of about how the `in`

operator works as a `True`

/`False`

check when placed in a loop or nested loop. There’s a lot of text but it’s all simple code and print output to be thorough.

The solution to this screen utilizes a loop and the `in`

operator set a dictionary key:value pair to `True`

or `False`

.

```
test_values = [1, 2, 5]
target_sums = [2, 3, 7, 8]
# possible_sums = {2, 3, 4, 6, 7, 10}
def find_sums_precompute(values, target_sums):
possible_sums = set()
for i in range(len(values)):
for j in range (i, len(values)):
possible_sums.add(values[i] + values[j])
sums = {}
for target in target_sums:
sums[target] = target in possible_sums
return sums, possible_sums
print(find_sums_precompute(test_values, test_targets))
# correct output:
# {2: True, 3: True, 7: True, 8: False}
```

So I was curious about the previous solution it improved upon.

```
test_values = [1, 2, 5]
target_sums = [2, 3, 7, 8]
# test_values possible sums = 2, 3, 4, 6, 7, 10
def find_sums(values, target_sums):
sums = {}
for target in target_sums:
sums[target] = False
for i in range(len(values)):
for j in range(i, len(values)):
if values[i] + values[j] == target:
sums[target] = True
return sums
print(find_sums(test_values, test_targets))
# same correct output:
# {2: True, 3: True, 7: True, 8: False}
```

In particular, why do we set the `sums[target] = False`

then later do this check `if values[i] + values[j] == target`

and switch to `True`

if true. So, I tried to implement it with the `in`

operator like the better solution `find_sums_precompute()`

, which failed.

```
def find_sums_with_in(values, target_sums):
sums = {}
for target in target_sums:
for i in range(len(values)):
for j in range(i, len(values)):
sums[target] = values[i] + values[j] in target_sums
return sums
print(find_sums_with_in(test_values, test_targets))
# Incorrect output returned
# {2: False, 3: False, 7: False, 8: False}
```

I printed out `target`

, `i`

's value, and` j`

’s value during the loops. Here is the result of the nested loop iteration through the first `target`

value, 2, which should result in `True`

.

*test_values = [1, 2, 5] used for loop 1 and 2*

*target_sums = [2, 3, 7, 8]*

*test_values possible sums = 2, 3, 4, 6, 7, 10*

target is 2

loop 1 value = 1

loop 2 value = 1

{2: True}

loop 1 value = 1

loop 2 value = 2

{2: True}

loop 1 value = 1

loop 2 value = 5

{2: False}

loop 1 value = 2

loop 2 value = 2

{2: False}

loop 1 value = 2

loop 2 value = 5

{2: True}

loop 1 value = 5

loop 2 value = 5

**{2: False}**

target is 3

The `in`

operator is working but it’s continuing through the nested loop in its entirety, therefore returning the last check, which happens to be `False`

. It does this for all following `target`

values as well. **Why does using** `in`

**fail to return the correct results in this case but not** `find_sums_precompute()`

?

Out of curiosity I did a print test of the `in`

operator check from `find_sums_precompute()`

. This is the full output, which ends in the correct answer.

```
# The "in" operator loop from find_sums_precompute()
for target in target_sums:
sums[target] = target in possible_sums
# target = [2, 3, 7, 8]
# possible_sums = {2, 3, 4, 6, 7, 10}
```

target value= 2

{2: True}

target value= 3

{2: True, 3: True}

target value= 7

{2: True, 3: True, 7: True}

target value= 8

{2: True, 3: True, 7: True, 8: False}

Here, it appears the `in`

operator ends the loop when it finds a `True`

. If it didn’t, wouldn’t the check on `target = 2`

return `False`

as it continued through the list and eventually hit `possible_sums = 10`

?

I also did some simple tests with addition, which didn’t seem to break `in`

the same way. Is it something about double loops??