+ 1

# delete duplicates from list

#error Traceback (most recent call last): File "./Playground/file0.py", line 6, in <module> if l[i]==l[t] : IndexError: list index out of range can anybody explain why this error? #code starts l=['1','22','45','51','334','67','78','98','82','987','11','22','53','60','89','123','321','444','789','987','1000'] print(l,'\n',len(l)) for i in range(0,20) : for t in range(i+1,21) : if l[i]==l[t] : l.remove(l[t]) else : pass print(l,len(l))

21st Dec 2020, 3:40 PM
Sahil Jain
+ 7
I agree with rodwynnejones .By the way, the problem occurs because the list becomes out of range. The contain values of that list is not constant because you used remove statement to remove the duplicate.So the contained value of that list will decrease.As a result,it will become out of range when you set the range (i+1,21) .If you write the line like this: for t in range(i+1,19) : your code will work fine but not granted that if you change the value of that list. instead of 21 writing 19 works fine because the last list contains 19 element after removing the duplicates. Here is the code: https://code.sololearn.com/ca21a133A208
21st Dec 2020, 4:01 PM
The future is now thanks to science
+ 7
never..... never ever remove while iterate. Only pros if theyknow what they do.
21st Dec 2020, 5:41 PM
Oma Falk
+ 3
Your shortening the list as you going along so your then trying to access elements outside the range. The easiest way is to put the list into a set()...then back into a list(). You can also use dictionary dict.fromkeys(). to keep the same ordering.,
21st Dec 2020, 3:52 PM
rodwynnejones
+ 3
Deleting duplicates from list can be reached easier. To benefit comes the property of set, where they are not allowed. This example introduces it: https://code.sololearn.com/ccCfDEDbPIaq/?ref=app
21st Dec 2020, 6:30 PM
JaScript
+ 2
..another way to do it (manually) is to declare an new empty list, iterate over your original list using "for ...in...", the check if the element is in the new list, if it isn't, append it to the new list, if it is in the new list...just "continue".
21st Dec 2020, 4:09 PM
rodwynnejones
0
but what was the problem in using list? will try ur methods too.thanks
21st Dec 2020, 3:56 PM
Sahil Jain