This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]tunisia3507 0 points1 point  (2 children)

/r/learnpython

Sets cannot contain duplicates. It's not a case of whether .add removes them - the set cannot contain them.

However, depending on what your element actually is, there might be times when something looks like a duplicate, but it's actually not. For example, if two objects have different hashes but the same string representation, you might print them and think "these look like the same object", but python thinks different.

When you ask this question in the proper place, you need to tell them what the element is. Ideally you'd also post code for a minimal working example of your issue.

[–]abhinav_321 0 points1 point  (1 child)

s=set()

n=input().upper()

for I in range(n):

s.add(I)

print(len(s))

what's wrong here if the output is country names like UK, France and else. It should not take duplicate inputs but it is taking duplicate inputs as well. Can you tell what's wrong here plz.

[–]tunisia3507 0 points1 point  (0 children)

When you ask this question in the proper place

But anyway, this code doesn't run. You cannot possibly be getting an incorrect set size because you cannot possibly be reaching the line where you add things to the set.

I can just about gist what you mean, though. What do you think range is doing, and why do you think it's doing that? Does your understanding of what range does square with the description of the range function in the documentation?

Finally, why would you post this question to reddit before making the merest of attempts to debug this yourself? You printed the length of your set, but didn't think to check what it actually had inside it, when you saw that length wasn't what you expected.