14. Remove Duplicates - Explanation Please


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/4?curriculum_id=4f89dab3d788890003000096#

Hello folks, I've been stuck on this exercise for quite sometime and I managed to figure it out, but I hate to admit that I'm not exactly sure why my code works... I fumbled around until I finally changed my "if" statement to the correct variable: "new_list."

My question is how does Python know to remove duplicates via the "not in" part of my "if" statement?

Thank you!

def remove_duplicates(lst):
    new_lst = []
    
    for dupes in lst:
        if dupes not in new_lst:
            new_lst.append(dupes)
        
    print new_lst
    return new_lst  

remove_duplicates([1, 1, 2, 2])



#2

in is used to check if something is present in a list, so not in will check if it not present. If not present, append to new_list


#3

Straight forward enough. Thank you!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.