I don't get the Remove Duplicates lesson?


I checked the solution code, and I understand everything except this one line:

if i > outputlist[-1]:

I get that this is to determine whether it’s a different number or not and to append it if it is, but I don’t get why -1 is used.

It mean it works, but it’d be nice to know why.


using negative indexes, we can access a list from the right hand side:

print ['a', 'b', 'c'][-1] # output: c

so its useful for comparing with last value in list.


so I’m assuming it counts regularly because -0 isn’t a thing.


-0 is a thing, however it is the same value as +0 (adding / removing nothing has the same effect: no effect)
as for regular, i assume you mean starting at 1 rather than 0, but where you start counting is arbitrary
in any case, you should think of it as positions, not counting. 0 is zero distance from the start, therefore it is the first one. if you add 1 you reach the second. if you subtract one, you reach the previous one (but python doesn’t give the programmer access to memory that way, so instead it’s interpreted in another useful way)


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.