I'm trying to write my codes to delete duplicates in a list. Here is first one:
a = [2,2,1,1,1,2,3,6,6]
b = [2,2,1,1,1,2,3,6,6]
for x in a:
a.remove(x)
if x in a:
b.remove(x)
print('the list without duplicates is: ', b)
Unfortunately, it produces this result:
the list without duplicates is: [2, 1, 2, 3, 6]
Then I try to write the second one:
a = [2,2,1,1,1,2,3,6,6]
b = [2,2,1,1,1,2,3,6,6]
for i in range(len(a)):
for x in a:
a.remove(x)
if x in a:
b.remove(x)
print('the list without duplicates is: ', b)
And this second one produces the result as I expected:
the list without duplicates is: [1, 2, 3, 6]
I really dont see why the second one is different from the first one. In fact, if I apply for the list:
[2,2,1,1,2,3,6,6]
Both of them produces the same result:
the list without duplicates is: [1, 2, 3, 6]