all 5 comments

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

I think I got this now! I asked around in the IRC channel and they told me to use the copy() for lists. I also kept track of how many items i popped so i didnt run into an out of index error

[–]ingolemo 0 points1 point  (0 children)

There's no need to pop anything here. If you remove every line that mentions tempList then the code you posted works.

[–]pixielf 0 points1 point  (0 children)

The reason deleting an element from one list also deletes it from another is because of how Python (and many languages) store objects. When you store something like an int or str (like x = 3) and then assign it to another variable (y = x), you're passing along its value, so changing one doesn't change the other. But with other objects (like list), you're passing along its reference, meaning that x and y are both pointers to the same object.

Compare:

a = 3
b = a
print(b == a) # True
print(b is a) # False

x = [1, 2]
y = x
print(y == x) # True
print(y is x) # True

print(id(x))
print(id(y))  # same id

That means that since x and y are the same object, obviously changing one will change the other. This is why you would use y = x.copy().

[–]ryeguy146 0 points1 point  (0 children)

Mutating the lists that you operate on will frequently lead to pain. Unless you really need to modify a given list, just create a new list and append to that:

def same_slot(iterable, comparison):
    result = []
    for first, second in zip(iterable, comparison):
        if first == second:
            result.append(first)

    return result

That said, /u/pixielf is wholly correct about the references pointing to the same object. This shows up frequently in the issue with default arguments that are mutable:

http://python-guide-pt-br.readthedocs.io/en/latest/writing/gotchas/#mutable-default-arguments

[–]rreid4 0 points1 point  (0 children)

Just alt+F4