<story><title>Python: The Dictionary Playbook</title><url>http://blog.amir.rachum.com/post/39501813266/python-the-dictionary-playbook</url></story><parent_chain></parent_chain><comment><author>elbear</author><text>You mentioned it a bit, but I want to make it clear that, even if you don&apos;t use 2.7, you can still count by doing:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    from collections import defaultdict
    counter = defaultdict(int)
&lt;/code&gt;&lt;/pre&gt;
There is a difference though, because you have to count manually, i.e:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    for i in &apos;supercalifragilisticexpialidocius&apos;:
        counter[i] += 1
&lt;/code&gt;&lt;/pre&gt;
Also, because defaultdict accepts any callable, you can have a dict of counters by doing:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    counters = defaultdict(lambda: defaultdict(int))
    for word in [&apos;apple&apos;, &apos;berry&apos;, &apos;grape&apos;]:
        for letter in word:
            counters[word][letter] += 1
&lt;/code&gt;&lt;/pre&gt;
This is not very obvious, so I don&apos;t use it a lot, but sometimes it&apos;s the most elegant solution.</text></comment>
<story><title>Python: The Dictionary Playbook</title><url>http://blog.amir.rachum.com/post/39501813266/python-the-dictionary-playbook</url></story><parent_chain></parent_chain><comment><author>avolcano</author><text>You know a guide is good when it makes you want to go back and refactor old code. Great information, thanks :)</text></comment>