Skip to content

language

#261: The Counter Class

When working with data in Python, we often need to count occurrences of items in a list or a string. While we could write our own code to do this, Python comes with the collections module that provides a convenient and efficient way to handle this task: the Counter class.

#210: Cache Function Calls

If our application uses recursion, we may call the same function with the same values many times. When that function is side-effect free, we can save a lot of time by caching the result. And even better, in Python we can do that without much effort.

#209: Defaultdict

When we work with dictionaries that contains lists, we need to make sure that the list exists before we can add items. Or when we use a dictionary for counters, we must make sure that we have a value before we can increment it. While this can be done by hand, there is a much more elegant way in Python.

#207: Type Hints

Internally Python uses classes and therefore types everywhere. However, when we declare our methods, we omit the types and end up with limited functionality for our code editors. In this post we look at type hints and how they can improve our experience to write code.