Dictionaries and unicode
<p>I have recently started learning Python and have got stuck in a small project I was trying.
I have an array which contains data for my project, I wanted to link this by using the code.</p>

<pre><code>    &gt;&gt;&gt; keys = ['a', 'b', 'c']
    &gt;&gt;&gt; values = [1, 2, 3]
    &gt;&gt;&gt; dictionary = dict(zip(keys, values))
</code></pre>

<p>But for my project I need Japanese characters in my values array. Is there a method where I can have my Japanese characters in the Array? Also if I were to type the words in using unicode, how would I be able to display the words equivalent to the unicode in the dictionary
For example:</p>

<pre><code>    print(u'\4096')
</code></pre>

<p>Would work but if I were to print the entire dictionary as </p>

<pre><code>    print (dictionary)
</code></pre>

<p>It wouldn't display my Japanese Characters. How would I be able to get around this problem?</p>

<p>Extra: </p>

<p>Another Problem is that I have my first array instead as a list as it was required to store information put together, is there an alternative solution?</p>

<pre><code>    dictionary = dict(zip(file_content,japanese))
    TypeError: unhashable type: 'list'
</code></pre>

<p>There are two major flavours of Python - Python2 and Python3, and one of the differences between them is how they treat Unicode. As a rule-of-thumb, you should be using Python3, because it is a much better language and getting better over time. Most of the bigger libraries support it.</p>

<p>In Python3, this should just work:</p>

<pre><code>keys = ['a', 'b', 'c']
values = ['Ã¡', 'b', 'Ã§']
dictionary = dict(zip(keys, values))
print(dictionary) # {'a': 'Ã¡', 'b': 'b', 'c': 'Ã§'}
</code></pre>

<p>In Python2, the same thing should work, but you should be using "Unicode" strings, something like this:</p>

<pre><code>keys = ['a', 'b', 'c']
values = [u'Ã¡', u'b', u'Ã§']
dictionary = dict(zip(keys, values))
print(dictionary) # {'a': u'\xe1', 'c': u'\xe7', 'b': u'b'}
</code></pre>

<p>The strings properly store the unicode contents you want, they are just not printed nicely, due to the naivitÃ© of the print function. But if you want to print a key, for example, it will figure it out and do the right thing. </p>

<p>You will always have to take care of properly encoding the strings when you're writing output to files or other types of streams.</p>

<p><a href="http://www.diveintopython3.net/strings.html" rel="nofollow">This</a> is a pretty good introduction to the topic of Python and strings.</p>

python dictionary unicode