mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
clarify which kinds of tuples can be used as dictionary keys
This commit is contained in:
@@ -490,7 +490,7 @@ AttributeError: 'tuple' object has no attribute 'remove'</samp>
|
||||
<ul>
|
||||
<li>Tuples are faster than lists. If you’re defining a constant set of values and all you’re ever going to do with it is iterate through it, use a tuple instead of a list.
|
||||
<li>It makes your code safer if you “write-protect” data that doesn’t need to be changed. Using a tuple instead of a list is like having an implied <code>assert</code> statement that shows this data is constant, and that special thought (and a specific function) is required to override that.
|
||||
<li>Some tuples can be used as dictionary keys, as you’ll see later in this chapter. (Lists can never be used as dictionary keys.)
|
||||
<li>Some tuples can be used as dictionary keys (specifically, tuples that contain <i>immutable</i> values like strings, numbers, and other tuples). Lists can never be used as dictionary keys, because lists are not immutable.
|
||||
</ul>
|
||||
|
||||
<blockquote class=note>
|
||||
|
||||
Reference in New Issue
Block a user