mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 15:00:18 +00:00
add example about trying to swap keys and values in a dictionary with mutable values
This commit is contained in:
@@ -326,6 +326,16 @@ body{counter-reset:h1 3}
|
||||
<samp class=p>>>> </samp><kbd class=pp>{value:key for key, value in a_dict.items()}</kbd>
|
||||
<samp class=pp>{1: 'a', 2: 'b', 3: 'c'}</samp></pre>
|
||||
|
||||
<p>Of course, this only works if the values of the dictionary are immutable, like strings or tuples. If you try this with a dictionary that contains lists, it will fail most spectacularly.
|
||||
|
||||
<pre class=screen>
|
||||
<samp class=p>>>> </samp><kbd class=pp>a_dict = {'a': [1, 2, 3], 'b': 4, 'c': 5}</kbd>
|
||||
<samp class=p>>>> </samp><kbd class=pp>{value:key for key, value in a_dict.items()}</kbd>
|
||||
<samp class=traceback>Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
File "<stdin>", line 1, in <dictcomp>
|
||||
TypeError: unhashable type: 'list'</samp></pre>
|
||||
|
||||
<p class=a>⁂
|
||||
|
||||
<h2 id=setcomprehension>Set Comprehensions</h2>
|
||||
|
||||
Reference in New Issue
Block a user