# Mechanics of this if statement

Can some explain how the last if statement works in this chunk of code? if i > outputlist[-1]:

def remove_duplicates(inputlist):
if inputlist == []:
return []

# Sort the input list from low to high

``````inputlist = sorted(inputlist)
``````

# Initialize the output list, and give it the first value of the now-sorted input list

``````outputlist = [inputlist]
``````

# …any values that are greater than the last value of the output list

``````for i in inputlist:
if i > outputlist[-1]:
outputlist.append(i)

return outputlist
``````

print remove_duplicates([1, 2, 2, 5, 6, 7, 7])

`if i > outputlist[-1]:`

if `i` (values of `inputlist`) is greater than the last value of the `outputlist` then should be append to `outputlist`.

lets go through it…

`````` for i in inputlist:
if i > outputlist[-1]:
outputlist.append(i)
``````

here at first loop `i` is `1` and the last value of the `outputlist` is also `1`.so the condition is `False` and it’ll do nothing.
At next loop `i` is `2` and the last value of the `outputlist` is `1`.so it’ll append `2` to `outputlist`.
At third loop again `i` is 2 and last of the value of `outpulist` is also `2`[as outputlist contains 1,2.because it appened `2` at second loop].so it’ll do nothing and go to the next value of the inputlist.so on…

so if you consider then you’ll understand that it can never be greater than last value of the `outputlist` if the value is already appened.

you can use `print` statement inside loop to check how it works.