Bottom Page

Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 list without repetitions
#1
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.
Quote
#2
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]
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)