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


Messages In This Thread
list without repetitions - by sokadin - Jan-10-2019, 04:36 PM
RE: list without repetitions - by nilamo - Jan-10-2019, 05:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sample random, unique string pairs from a list without repetitions walterwhite 1 464 Nov-19-2023, 10:07 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020