14. Remove Duplicates - Explanation Please



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:
    print new_lst
    return new_lst  

remove_duplicates([1, 1, 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


Straight forward enough. Thank you!


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