1

First of all, I have the answer to the question from mym teacher (it's from a lecture), and I see that stackoverflow has tons of questions like that. But my question isn't about the solution, but the meaning behind it:

Write a function that given a list of elements, return a new list with no duplicates.

def no_duplicate_list(lst):                                                             
    new_lst = []                                                                        
    for i in lst:                                                                       
        if lst.count(i) == 1:                                                          
            new_lst.append(i)                                                          
        elif lst.count(i) > 1:                                                         
            lst.remove(i)                                                              
    return new_lst   
                                                            

print(no_duplicate_list([1,2,3,4,5, 3, 4, 2])) 

 

I need to know, why when I remove an index from a list, let's say I got to the second index ( lst[1] = 2), when I remove it, it skips right after it to lst[3]4 and does not reach lst[2]=3.

  1. Why is this happening?
  2. How am I supposed to make it so that if I remove it, it won't skipß through lst[2]=3 (although in the removed lst, it moves to lst[1]=3, that is probably the reason, but how do I fix it?)
jps
  • 20,041
  • 15
  • 75
  • 79
  • 1
    Have a read through https://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list-while-iterating-over-it - that should explain it – Jon Clements Apr 23 '22 at 10:25
  • If that doesn't help - then you might want to consider making an [edit] as to what you don't understand - but that explains it - you might have to read through it and experiment a few times though – Jon Clements Apr 23 '22 at 10:26
  • @TryingToMath `lst.remove(i)` doesn't remove the index instead it removes the first occurrence of that element. – Abhyuday Vaish Apr 23 '22 at 10:28
  • @JonClements The problem is, that in their answers, they are using list comprehension, we didnt study it yet and we wont I think, since its a introduction to python course. I can learn list comprehension, I already tried before, it wasnt easy, but still its complicated. And yea, he has the same problem as me, I saw what the answer said, my thing exactly. – Analysis_Complex_Study Apr 23 '22 at 10:28
  • @AbhyudayVaish yea, it removes the first occurence, that I know :) thats why, I tried to delete it, but instead, keep the indexes, but I see its a problem. – Analysis_Complex_Study Apr 23 '22 at 10:29
  • I can just see the answer of my professor and know how to do it, but I really don't want to :( I want to learn python, its the most hard course in the academy ( for most of people I think ). – Analysis_Complex_Study Apr 23 '22 at 10:31
  • I guess also I could answer it pretty easily, but it will be a long code with efficiency - almost zero, I can make lot of if's and such, but I feel that wont be the point. Should I just check the professor answer and see what he did? I saw that also the person on the link said there is no good way to alter through a list, so if a experienced person said it, I guess there is no way a begineer will find one :\ – Analysis_Complex_Study Apr 23 '22 at 10:35
  • please don't add *answered*, *solved* or similar words to the title. Instead you can accept the best answer by clicking on the checkmark on the left side of the answer. – jps Apr 23 '22 at 12:56
  • @jps Oh sorry, my bad, I wont do it again. Thanks for notifying me!! – Analysis_Complex_Study Apr 25 '22 at 15:45

2 Answers2

1

You are modifying a list while iterating over it. That will invariably lead to undesired results.

Have you considered using a set?

def no_duplicate_list(lst):
    return list(set(lst))

Here's a less efficient way without using a set:

def no_duplicate_list(lst):
    new_list = []
    for e in lst:
        if not e in new_list:
            new_list.append(e)
    return new_list
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
  • Actually we learned set on the lecture before it, I dont know if the answer the professor did was with set, could be ( didnt see ), but I am really not good with using tuples\set and dictionarys, I thought i could do it using list. I saw this solution also at stackvoverflow, but I think he wanted us to try to do it with list somehow. – Analysis_Complex_Study Apr 23 '22 at 10:32
  • @TryingToMath I've added another way to do it to the answer. This could be very inefficient if the list is large. It will however retain order whereas the set-based approach might not – DarkKnight Apr 23 '22 at 10:41
  • oh actually it is a great idea, yea you are right about what you said, but it is also a solution to think of. I still want to understand about the alterating thought, but its also good, thanks! – Analysis_Complex_Study Apr 23 '22 at 10:43
  • wanted to notify you, seems the teacher did the same as you, not in, its not very efficient, but its seems he did the same :) – Analysis_Complex_Study Apr 23 '22 at 11:17
0

You should never modify the List as you are parsing it. Otherwise the iterator will be lost.

Here, indeed, this is useless since you will put the elements in your new List, that you will return.

The straightforward way is to take one element, check if you have it in your new List, if not, adding it.

def no_duplicate_list(lst):                                                             
    new_lst = []                                                                        
    for i in lst:                                                                       
        if i not in new_lst:
            new_lst.append(i)                                                                                                                   
    return new_lst   
Floh
  • 745
  • 3
  • 16
  • 1
    Ahh, its the same soltuion Lancelot did. So there isnt any efficient way of doing it I guess. ( instead of using sets of course ) Thanks :) – Analysis_Complex_Study Apr 23 '22 at 10:57
  • wanted to notify you, seems the teacher did the same as you, not in, its not very efficient, but its seems he did the same :) – Analysis_Complex_Study Apr 23 '22 at 11:17
  • Why do you say it that `not in ` is not efficient ? – Floh Apr 23 '22 at 11:58
  • Anyway, Here this is a naïve solution. I prefer always start with the naïve one and optimize where it is needed. Here, the best is probably to use set whose implémentation is very efficient. – Floh Apr 23 '22 at 11:59
  • 1
    Oh sorry, I didnt mean not efficient. I mean, it is efficient, but if it is a long list it will be less efficient. sorry, I needed to say it. but if it is a short list, its very good. – Analysis_Complex_Study Apr 23 '22 at 12:40