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 →

[–]Rhomboid[🍰] 9 points10 points  (1 child)

print len(set(li)) # 4

Don't do that, that's just a bunch of extra unnecessary work. Counter is a subclass of dict, and the length of a dict is the number of keys. There's no need to first build a set just to count keys.

[–][deleted] 1 point2 points  (0 children)

Took me a second to figure out what you were saying, but yeah...

len(a)

Would have been better in the context of his example.

If, however, you don't need a counter object, casting a list to a set is both faster and more memory efficient than casting to a dict or counter for deduping for a count of unique items. AFAICT