list without repetitions - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: list without repetitions (/thread-15272.html) |
list without repetitions - sokadin - Jan-10-2019 Good day dear people, i just need little help with my code. I have to make list without repetitions from the one that has repetitions. I don't know where am i making a mistake. this is code: liss=[6,7,5,3,6,8,9,6,4,3,2,5,6,8,9,0,1,98] def lwr(liss): liss.sort(key=None,reverse=False) print(liss) i=1 liss1=[] for n in liss: if n!=liss[i]: liss1.append(n) i+=1 else: continue return print(liss1) lwr(liss)Thank you. RE: list without repetitions - nilamo - Jan-10-2019 You generate a new list, but then don't return it. 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) Unless you need to use indexes (for homework), I think you should just compare with the last value appended. ie: if n != liss1[-1]
|