Remove Quotation marks from a dictionary
<p>I have a dictionary of bigrams, obtained by importing a csv and transforming it to a dictionary:</p>

<pre><code>bigram_dict = {"('key1', 'key2')": 'meaning', "('key22', 'key13')": 'mean2'}
</code></pre>

<p>I want keys' dictionary to be without quotation marks, i.e.:</p>

<pre><code>desired_bigram_dict={('key1', 'key2'): 'meaning', ('key22', 'key13'): 'mean2'}
</code></pre>

<p>Would you please suggest me how to do this?</p>

<p>You can <a href="https://docs.python.org/2/library/ast.html#ast.literal_eval" rel="nofollow"><em>literal_eval</em></a> each key and reassign:</p>

<pre><code>from ast import literal_eval

bigram_dict = {"('key1', 'key2')": 'meaning', "('key22', 'key13')": 'mean2'}


for k,v in bigram_dict.items():
    bigram_dict[literal_eval(k)] = v
</code></pre>

<p>Or to create a new dict, just use the same logic with a <em>dict comprehension</em>:</p>

<pre><code>{literal_eval(k):v for k,v in bigram_dict.items()}
</code></pre>

<p>Both will give you:</p>

<pre><code>{('key1', 'key2'): 'meaning', ('key22', 'key13'): 'mean2'}
</code></pre>

python nltk