A list of python snippets I find interesting
-
The Python standard library has an implementation of
Counter
that is typically used to find the most common items in a a collection, with their count. I find the implement ofcollections.Counter
instructive in general. I especially like the implementation ofmost_common
link. I find it interesting howmost_common
determines which method to use for efficiency reasons (based on n). -
In
heapq
module (standard library), implemention ofnsmallest
link. In general, the heapq module has awesome comments. This is true fornsmallest
method also. Also, I like how this method is lazy (stores only k items irrespective of input) and generic (the optional key).