Jan-10-2019, 05:49 PM
You generate a new list, but then don't return it.
But also, you only increment
To demonstrate, I added a couple prints, along with a smaller sample size:
But also, you only increment
i
when a match is found. Which means once there isn't a match, the whole rest of the list would be included.To demonstrate, I added a couple prints, along with a smaller sample size:
def lwr(liss): liss.sort(key=None, reverse=False) print(liss) i=1 liss1=[] for n in liss: print(f"Index: {i}, Current Value: {n}, Next Value: {liss[i]}") if n!=liss[i]: liss1.append(n) i+=1 return liss1 liss=[1, 1, 2, 3, 3, 4, 5, 5] unique = lwr(liss) print(unique)
Output:[1, 1, 2, 3, 3, 4, 5, 5]
Index: 1, Current Value: 1, Next Value: 1
Index: 1, Current Value: 1, Next Value: 1
Index: 1, Current Value: 2, Next Value: 1
Index: 2, Current Value: 3, Next Value: 2
Index: 3, Current Value: 3, Next Value: 3
Index: 3, Current Value: 4, Next Value: 3
Index: 4, Current Value: 5, Next Value: 3
Index: 5, Current Value: 5, Next Value: 4
[2, 3, 4, 5, 5]
Unless you need to use indexes (for homework), I think you should just compare with the last value appended. ie: if n != liss1[-1]