Bottom Page

• 1 Vote(s) - 1 Average
• 1
• 2
• 3
• 4
• 5
 list without repetitions sokadin Unladen Swallow Posts: 1 Threads: 1 Joined: Jan 2019 Reputation: 0 Likes received: 0 #1 Jan-10-2019, 04:36 PM 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. nilamo Last Thursdayist Posts: 3,106 Threads: 79 Joined: Sep 2016 Reputation: 124 Likes received: 686 #2 Jan-10-2019, 05:49 PM 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)`````````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]` « Next Oldest | Next Newest »

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)