[Web Feed] Python???s??defaultdict??is an incredibly useful tool to specify default values f...

Feed Supplier <[email protected]> Mon, 10 May 21 01:28:32 UTC
Newsgroups alt.comp.programming
Organization Usenet.Farm
Message-ID <[email protected]>
..or missing keys (RealPython has a great primer on them).I



Python’s 


defaultdict


 is an incredibly useful tool to specify default values for missing keys (RealPython has a great primer on them).

I found I was taking up so much memory to store the value 0 for an algorithm which counted rather infrequent elements in a huge sequence split into many chunks. Using a 


defaultdict(int)


 for each distinct element to store which indices had a non-zero count of the respective element greatly optimised it, but I yearned for a 


defaultlist


 equivalent to standardise this approach and generally make my code more expressive.




Link 1


Implementing a defaultlist – Matthew's Blog: https://blog.matthewbarber.io/2020/12/08/defaultlist