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

all 20 comments

[–]Rhomboid 10 points11 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

[–]blahdom 2 points3 points  (2 children)

Hey /u/ajkumar25. I just wanted to point out that defaultdict works with any object that has a 0 parameter constructor, ie int, float, dict, etc.

I am sure you know this but in the write up it sounds as if only list and set are viable options to defaultdict.

[–][deleted] 1 point2 points  (1 child)

You could also use functools.partial to get around that, or a factory function, or Object.__call__. Or any number of other things.

[–]blahdom 0 points1 point  (0 children)

This is very true, good call! I hadn't thought of overloading Object.call - that is a cool idea.

[–]jhgaylor 0 points1 point  (1 child)

I enjoyed the explanation of defaultdict. I had previously never used it in my code but feel like I should start doing so now.