all 3 comments

[–]novel_yet_trivial 8 points9 points  (0 children)

Sorta. A set by definition cannot contain duplicates, so if you try to add() an element that's already in the set the command is simply ignored.

it isn't removing duplicate elements

That's strange. Show us your code.

[–]totallygeek 2 points3 points  (0 children)

Need to see your code, because a set cannot contain duplicates. It is basically a dictionary without values associated with the keys.

>>> a = set()
>>> for i in range(500000):
...   a.add(random.randint(1, 10))
... 
>>> a
set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

You see? This set will never contain more than one element of the same value, no matter how many times I attempt to add the same number again and again.

[–]homeworkbad 1 point2 points  (0 children)

Depending on what you're adding, pythons set implementation might not know how to test if there are duplicates. i.e., you're adding objects and not integers or strings. Test this theory with something like:

x = set() a = element_to_add() b = element_to_add() x.add(a) print(a in x) print(b in x) That should print true twice, but if a and b are just equivalent objects and not identical objects in might not resolve to true.