<div><h1id="comprehensivepythoncheatsheet">Comprehensive Python Cheatsheet</h1><pclass="banner"><sup><ahref="https://raw.githubusercontent.com/gto76/python-cheatsheet/main/README.md">Download text file</a>, <ahref="https://transactions.sendowl.com/products/78175486/4422834F/view">Buy PDF</a>, <ahref="https://github.com/gto76/python-cheatsheet">Fork me on GitHub</a>, <ahref="https://github.com/gto76/python-cheatsheet/wiki/Frequently-Asked-Questions">Check out FAQ</a> or <ahref="index.html?theme=dark3">Switch to dark theme</a>.
<div><h2id="list"><ahref="#list"name="list">#</a>List</h2><pre><codeclass="python language-python hljs"><el> = <list>[index] <spanclass="hljs-comment"># First index is 0. Last -1. Allows assignments.</span>
<pre><codeclass="python language-python hljs"><el> = <list>[index]<spanclass="hljs-comment"># First index is 0. Last -1. Allows assignments.</span>
<list> = <list>[<slice>] <spanclass="hljs-comment"># Also <list>[from_inclusive : to_exclusive : ±step].</span>
</code></pre>
<pre><codeclass="python language-python hljs"><list>.sort() <spanclass="hljs-comment"># Sorts in ascending order.</span>
<pre><codeclass="python language-python hljs"><list>.append(<el>) <spanclass="hljs-comment"># Appends element to the end. Also <list> += [<el>].</span>
<list>.extend(<collection>) <spanclass="hljs-comment"># Appends elements to the end. Also <list> += <coll>.</span>
</code></pre>
<pre><codeclass="python language-python hljs"><list>.sort() <spanclass="hljs-comment"># Sorts elements in ascending order.</span>
<list>.reverse() <spanclass="hljs-comment"># Reverses the list in-place.</span>
<list> = sorted(<collection>) <spanclass="hljs-comment"># Returns a new sorted list.</span>
<list>.remove(<el>) <spanclass="hljs-comment"># Removes first occurrence of the item or raises ValueError.</span>
<list>.clear() <spanclass="hljs-comment"># Removes all items. Also works on dictionary and set.</span>
</code></pre>
<div><h2id="dictionary"><ahref="#dictionary"name="dictionary">#</a>Dictionary</h2><pre><codeclass="python language-python hljs"><view> = <dict>.keys() <spanclass="hljs-comment"># Coll. of keys that reflects changes.</span>
<view> = <dict>.values() <spanclass="hljs-comment"># Coll. of values that reflects changes.</span>
<view> = <dict>.items() <spanclass="hljs-comment"># Coll. of key-value tuples that reflects chgs.</span>
<div><h2id="dictionary"><ahref="#dictionary"name="dictionary">#</a>Dictionary</h2><pre><codeclass="python language-python hljs"><dict> = {key_1: val_1, key_2: val_2, ...} <spanclass="hljs-comment"># Use `<dict>[key]` to get or set the value.</span>
</code></pre></div>
<pre><codeclass="python language-python hljs"><view> = <dict>.keys() <spanclass="hljs-comment"># Coll. of keys that reflects changes.</span>
<view> = <dict>.values() <spanclass="hljs-comment"># Coll. of values that reflects changes.</span>
<view> = <dict>.items() <spanclass="hljs-comment"># Coll. of key-value tuples that reflects chgs.</span>
</code></pre>
<pre><codeclass="python language-python hljs">value = <dict>.get(key, default=<spanclass="hljs-keyword">None</span>) <spanclass="hljs-comment"># Returns default if key is missing.</span>
value = <dict>.setdefault(key, default=<spanclass="hljs-keyword">None</span>) <spanclass="hljs-comment"># Returns and writes default if key is missing.</span>
<dict> = collections.defaultdict(<type>) <spanclass="hljs-comment"># Returns a dict with default value `<type>()`.</span>
@ -154,13 +157,11 @@ value = <dict>.pop(key) <span class="hljs-comment"