Member-only story
Python: Hidden Features — part 4

Hi Everyone, In this article I will go throw couple of techniques or features which anyone can use in day to day coding.
In my journey through these Python features, I’ve uncovered tools that go beyond the ordinary, adding a touch of magic to our code.
Lets get started —
1. functools.lru_cache
— Boost Performance with Caching
The lru_cache
decorator from functools
helps cache function results, reducing redundant computations. This is especially useful in recursive functions like Fibonacci sequence calculations, where repeated calls to the same input can be avoided.
from functools import lru_cache
@lru_cache(maxsize=32)
def fibonacci(n):
if n < 2:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
Significantly improves performance by avoiding repeated calculations and storing previous results for quick retrieval.
2. collections.Counter
— Quick Count for Anything
Counter
from the collections
module is a powerful tool to count occurrences of elements in an iterable.
from collections import Counter
count = Counter(['apple'…