डिफ़ॉल्ट डिफेंडर का पैरामीटर फ़ंक्शन है जिसे नए तत्वों के निर्माण के लिए बुलाया जाएगा। तो चलो एक मेमने का उपयोग करें!
>>> from collections import defaultdict
>>> d = defaultdict(lambda : defaultdict(int))
>>> print d[0]
defaultdict(<type 'int'>, {})
>>> print d[0]["x"]
0
पायथन 2.7 के बाद से, ए काउंटर का उपयोग करके भी बेहतर समाधान है :
>>> from collections import Counter
>>> c = Counter()
>>> c["goodbye"]+=1
>>> c["and thank you"]=42
>>> c["for the fish"]-=5
>>> c
Counter({'and thank you': 42, 'goodbye': 1, 'for the fish': -5})
कुछ बोनस सुविधाएँ
>>> c.most_common()[:2]
[('and thank you', 42), ('goodbye', 1)]
अधिक जानकारी के लिए PyMOTW - संग्रह - कंटेनर डेटा प्रकार और पायथन प्रलेखन - संग्रह देखें