Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can't add a list to a set because lists are mutable, meaning that you can change the contents of the list after adding it to the set.</p> <p>You can however add tuples to the set, because you cannot change the contents of a tuple:</p> <pre><code>&gt;&gt;&gt; a.add(('f', 'g')) &gt;&gt;&gt; print a set(['a', 'c', 'b', 'e', 'd', ('f', 'g')]) </code></pre> <hr> <p><strong>Edit</strong>: some explanation: The documentation defines a <code>set</code> as <em>an unordered collection of distinct hashable objects.</em> The objects have to be hashable so that finding, adding and removing elements can be done faster than looking at each individual element every time you perform these operations. The specific algorithms used are explained in the <a href="http://en.wikipedia.org/wiki/Hash_tree" rel="noreferrer">Wikipedia article</a>. Pythons hashing algorithms are explained on <a href="http://effbot.org/zone/python-hash.htm" rel="noreferrer">effbot.org</a> and pythons <code>__hash__</code> function in the <a href="http://docs.python.org/reference/datamodel.html#object.__hash__" rel="noreferrer">python reference</a>.</p> <p>Some facts:</p> <ul> <li><strong>Set elements</strong> as well as <strong>dictionary keys</strong> have to be hashable</li> <li>Some unhashable datatypes: <ul> <li><code>list</code>: use <code>tuple</code> instead</li> <li><code>set</code>: use <code>frozenset</code> instead</li> <li><code>dict</code>: has no official counterpart, but there are some <a href="https://stackoverflow.com/questions/1151658/python-hashable-dicts">recipes</a></li> </ul></li> <li>Object instances are hashable by default with each instance having a unique hash. You can override this behavior as explained in the python reference.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload