First of all, I have the answer to the question from mym teacher (it's from a lecture), and I see that stackoverflow has tons of questions like that. But my question isn't 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, let's 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.
- Why is this happening?
- How am I supposed to make it so that if I remove it, it won't skipß through lst[2]=3 (although in the removed lst, it moves to lst[1]=3, that is probably the reason, but how do I fix it?)