Home > Mobile >  Python - no duplicated list ( begineer ) Question regarding slicing of list
Python - no duplicated list ( begineer ) Question regarding slicing of list

Time:04-23

Okay, first of all, I have the answer of my teacher of the question ( its from a lecture ), and I see that stackoverflow has ton of questions like that. But my question isnt 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, lets 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 wont skip through lst[2]=3 ( althoght in the removed lst, it moves to lst[1]=3, that is probably the reason, but how do I fix it? )

Please dont downgrade the post, I really want to learn and I was told the community here is harsh regarding the questions ( which is why I really was trying to simplify the question as much as i could ). Thanks.

CodePudding user response:

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

CodePudding user response:

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   
  • Related