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