Remove Duplicates


#1



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


Traceback (most recent call last):
File "python", line 13, in
File "python", line 7, in remove_duplicates
IndexError: list index out of range


Can some one please help me with why my code does not work? :slight_smile:


def remove_duplicates(x):
    lst= x
    count =1
    for n in lst:
        if count< len(lst):
            for m in range(count, len(lst)):
                if n==lst[m]:
                    del(lst[m])
        count=count+1
    print lst
    return lst
a= [1, 1, 2, 2]
remove_duplicates(a)


#2

Have you had a look at your error message? It's telling you what and where

Do note that changing the size of your list will affect loops currently iterating through it.
You'll have an easier time creating a new list with only the values to keep


#3

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