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 of
collections.Counterinstructive in general. I especially like the implementation of
most_commonlink. I find it interesting how
most_commondetermines which method to use for efficiency reasons (based on n).
heapqmodule (standard library), implemention of
nsmallestlink. In general, the heapq module has awesome comments. This is true for
nsmallestmethod also. Also, I like how this method is lazy (stores only k items irrespective of input) and generic (the optional key).