Initializing python dictionary with new keys
<p>How can I initialise a python dictionary with keys that are not pre-determined? </p>

<p>If I just initialise the dictionary with <code>dict = {}</code>, it gives me a Key Error once I try to populate it with new key-value pairs.</p>

<p>A workaround would be to do a try-except so that it tries to access existing keys first or initialises the dictionary for a new key if the former fails. A concrete example would be counting words in a text (this code example would lead to a <code>Key Error</code>): </p>

<pre><code>wordcount = {}
for word in text:
     wordcount[word] += 1
</code></pre>