Skip to content

Commit 2873f96

Browse files
committed
List, Dict, Set - big changes
1 parent 300a29c commit 2873f96

File tree

2 files changed

+64
-64
lines changed

2 files changed

+64
-64
lines changed

README.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,37 @@ Contents
2121
Main
2222
----
2323
```python
24-
if __name__ == '__main__': # Skips next line if file was imported.
25-
main() # Runs `def main(): ...` function.
24+
if __name__ == '__main__': # Skips indented lines of code if file was imported.
25+
main() # Executes user-defined `def main(): ...` function.
2626
```
2727

2828

2929
List
3030
----
3131
```python
32-
<list> = [<el_1>, <el_2>, ...] # Creates a list object. Also list(<collection>).
32+
<list> = [<el_1>, <el_2>, ...] # Creates a new list object. Also list(<collection>).
3333
```
3434

3535
```python
36-
<el> = <list>[index] # First index is 0. Last -1. Allows assignments.
36+
<el> = <list>[index] # First index is 0, last -1. Also `<list>[i] = <el>`.
3737
<list> = <list>[<slice>] # Also <list>[from_inclusive : to_exclusive : ±step].
3838
```
3939

4040
```python
41-
<list>.append(<el>) # Appends element to the end. Also <list> += [<el>].
42-
<list>.extend(<collection>) # Appends elements to the end. Also <list> += <coll>.
41+
<list>.append(<el>) # Appends element to the end. Also `<list> += [<el>]`.
42+
<list>.extend(<collection>) # Appends multiple elements. Also `<list> += <coll>`.
4343
```
4444

4545
```python
46-
<list>.sort() # Sorts the elements in ascending order.
47-
<list>.reverse() # Reverses the order of list's elements.
48-
<list> = sorted(<collection>) # Returns a new list with sorted elements.
49-
<iter> = reversed(<list>) # Returns reversed iterator of elements.
46+
<list>.sort(reverse=False) # Sorts the elements of the list in ascending order.
47+
<list>.reverse() # Reverses the order of elements. Takes linear time.
48+
<list> = sorted(<collection>) # Returns a new sorted list. Accepts `reverse=True`.
49+
<iter> = reversed(<list>) # Returns reversed iterator. Also list(<iterator>).
5050
```
5151

5252
```python
53-
<el> = max(<collection>) # Returns largest element. Also min(<el_1>, ...).
54-
<num> = sum(<collection>) # Returns sum of elements. Also math.prod(<coll>).
53+
<el> = max(<collection>) # Returns the largest element. Also min(<el_1>, ...).
54+
<num> = sum(<collection>) # Returns a sum of elements. Also math.prod(<coll>).
5555
```
5656

5757
```python
@@ -70,7 +70,7 @@ flatter_list = list(itertools.chain.from_iterable(<list>))
7070
<int> = <list>.index(<el>) # Returns index of the first occurrence or raises ValueError.
7171
<el> = <list>.pop() # Removes and returns item from the end or at index if passed.
7272
<list>.insert(<int>, <el>) # Inserts item at passed index and moves the rest to the right.
73-
<list>.remove(<el>) # Removes first occurrence of the item or raises ValueError.
73+
<list>.remove(<el>) # Removes the first occurrence or raises ValueError exception.
7474
<list>.clear() # Removes all list's items. Also works on dictionary and set.
7575
```
7676

@@ -88,16 +88,16 @@ Dictionary
8888
```
8989

9090
```python
91-
value = <dict>.get(key, default=None) # Returns default argument if key is missing.
91+
value = <dict>.get(key, default=None) # Returns argument default if key is missing.
9292
value = <dict>.setdefault(key, default=None) # Returns and writes default if key is missing.
93-
<dict> = collections.defaultdict(<type>) # Returns a dict with default value `<type>()`.
94-
<dict> = collections.defaultdict(lambda: 1) # Returns a dict with default value 1.
93+
<dict> = collections.defaultdict(<type>) # Dict with automatic default value `<type>()`.
94+
<dict> = collections.defaultdict(lambda: 1) # Dictionary with automatic default value 1.
9595
```
9696

9797
```python
9898
<dict> = dict(<collection>) # Creates a dict from coll. of key-value pairs.
99-
<dict> = dict(zip(keys, values)) # Creates a dict from two collections.
100-
<dict> = dict.fromkeys(keys [, value]) # Creates a dict from collection of keys.
99+
<dict> = dict(zip(keys, values)) # Creates a dictionary from two collections.
100+
<dict> = dict.fromkeys(keys [, value]) # Creates a dictionary from collection of keys.
101101
```
102102

103103
```python
@@ -120,32 +120,32 @@ value = <dict>.pop(key) # Removes item or raises KeyErro
120120
Set
121121
---
122122
```python
123-
<set> = {<el_1>, <el_2>, ...} # Use `set()` for empty set.
123+
<set> = {<el_1>, <el_2>, ...} # Coll. of unique items. Also set(), set(<coll>).
124124
```
125125

126126
```python
127-
<set>.add(<el>) # Or: <set> |= {<el>}
128-
<set>.update(<collection> [, ...]) # Or: <set> |= <set>
127+
<set>.add(<el>) # Adds item to the set. Also `<set> |= {<el>}`.
128+
<set>.update(<collection> [, ...]) # Adds items to the set. Also `<set> |= <set>`.
129129
```
130130

131131
```python
132-
<set> = <set>.union(<coll.>) # Or: <set> | <set>
133-
<set> = <set>.intersection(<coll.>) # Or: <set> & <set>
134-
<set> = <set>.difference(<coll.>) # Or: <set> - <set>
135-
<set> = <set>.symmetric_difference(<coll.>) # Or: <set> ^ <set>
136-
<bool> = <set>.issubset(<coll.>) # Or: <set> <= <set>
137-
<bool> = <set>.issuperset(<coll.>) # Or: <set> >= <set>
132+
<set> = <set>.union(<coll>) # Returns a set of all items. Also <set> | <set>.
133+
<set> = <set>.intersection(<coll>) # Returns all shared items. Also <set> & <set>.
134+
<set> = <set>.difference(<coll>) # Returns set's unique items. Also <set> - <set>.
135+
<set> = <set>.symmetric_difference(<coll>) # Returns non-shared items. Also <set> ^ <set>.
136+
<bool> = <set>.issuperset(<coll>) # Returns False if collection has unique items.
137+
<bool> = <set>.issubset(<coll>) # Is collection a superset? Also <set> <= <set>.
138138
```
139139

140140
```python
141-
<el> = <set>.pop() # Raises KeyError if empty.
142-
<set>.remove(<el>) # Raises KeyError if missing.
143-
<set>.discard(<el>) # Doesn't raise an error.
141+
<el> = <set>.pop() # Removes and returns an item or raises KeyError.
142+
<set>.remove(<el>) # Removes the item or raises KeyError if missing.
143+
<set>.discard(<el>) # Same as remove() but it doesn't raise an error.
144144
```
145145

146146
### Frozen Set
147147
* **Is immutable and hashable.**
148-
* **That means it can be used as a key in a dictionary or as an element in a set.**
148+
* **That means it can be used as a key in a dictionary or as an item in a set.**
149149
```python
150150
<frozenset> = frozenset(<collection>)
151151
```

index.html

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
<body>
5858
<header>
59-
<aside>September 16, 2025</aside>
59+
<aside>October 5, 2025</aside>
6060
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
6161
</header>
6262

@@ -107,26 +107,26 @@
107107

108108

109109

110-
<div><h2 id="main"><a href="#main" name="main">#</a>Main</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>: <span class="hljs-comment"># Skips next line if file was imported.</span>
111-
main() <span class="hljs-comment"># Runs `def main(): ...` function.</span>
110+
<div><h2 id="main"><a href="#main" name="main">#</a>Main</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>: <span class="hljs-comment"># Skips indented lines of code if file was imported.</span>
111+
main() <span class="hljs-comment"># Executes user-defined `def main(): ...` function.</span>
112112
</code></pre></div>
113113

114-
<div><h2 id="list"><a href="#list" name="list">#</a>List</h2><pre><code class="python language-python hljs">&lt;list&gt; = [&lt;el_1&gt;, &lt;el_2&gt;, ...] <span class="hljs-comment"># Creates a list object. Also list(&lt;collection&gt;).</span>
114+
<div><h2 id="list"><a href="#list" name="list">#</a>List</h2><pre><code class="python language-python hljs">&lt;list&gt; = [&lt;el_1&gt;, &lt;el_2&gt;, ...] <span class="hljs-comment"># Creates a new list object. Also list(&lt;collection&gt;).</span>
115115
</code></pre></div>
116116

117-
<pre><code class="python language-python hljs">&lt;el&gt; = &lt;list&gt;[index] <span class="hljs-comment"># First index is 0. Last -1. Allows assignments.</span>
117+
<pre><code class="python language-python hljs">&lt;el&gt; = &lt;list&gt;[index] <span class="hljs-comment"># First index is 0, last -1. Also `&lt;list&gt;[i] = &lt;el&gt;`.</span>
118118
&lt;list&gt; = &lt;list&gt;[&lt;slice&gt;] <span class="hljs-comment"># Also &lt;list&gt;[from_inclusive : to_exclusive : ±step].</span>
119119
</code></pre>
120-
<pre><code class="python language-python hljs">&lt;list&gt;.append(&lt;el&gt;) <span class="hljs-comment"># Appends element to the end. Also &lt;list&gt; += [&lt;el&gt;].</span>
121-
&lt;list&gt;.extend(&lt;collection&gt;) <span class="hljs-comment"># Appends elements to the end. Also &lt;list&gt; += &lt;coll&gt;.</span>
120+
<pre><code class="python language-python hljs">&lt;list&gt;.append(&lt;el&gt;) <span class="hljs-comment"># Appends element to the end. Also `&lt;list&gt; += [&lt;el&gt;]`.</span>
121+
&lt;list&gt;.extend(&lt;collection&gt;) <span class="hljs-comment"># Appends multiple elements. Also `&lt;list&gt; += &lt;coll&gt;`.</span>
122122
</code></pre>
123-
<pre><code class="python language-python hljs">&lt;list&gt;.sort() <span class="hljs-comment"># Sorts the elements in ascending order.</span>
124-
&lt;list&gt;.reverse() <span class="hljs-comment"># Reverses the order of list's elements.</span>
125-
&lt;list&gt; = sorted(&lt;collection&gt;) <span class="hljs-comment"># Returns a new list with sorted elements.</span>
126-
&lt;iter&gt; = reversed(&lt;list&gt;) <span class="hljs-comment"># Returns reversed iterator of elements.</span>
123+
<pre><code class="python language-python hljs">&lt;list&gt;.sort(reverse=<span class="hljs-keyword">False</span>) <span class="hljs-comment"># Sorts the elements of the list in ascending order.</span>
124+
&lt;list&gt;.reverse() <span class="hljs-comment"># Reverses the order of elements. Takes linear time.</span>
125+
&lt;list&gt; = sorted(&lt;collection&gt;) <span class="hljs-comment"># Returns a new sorted list. Accepts `reverse=True`.</span>
126+
&lt;iter&gt; = reversed(&lt;list&gt;) <span class="hljs-comment"># Returns reversed iterator. Also list(&lt;iterator&gt;).</span>
127127
</code></pre>
128-
<pre><code class="python language-python hljs">&lt;el&gt; = max(&lt;collection&gt;) <span class="hljs-comment"># Returns largest element. Also min(&lt;el_1&gt;, ...).</span>
129-
&lt;num&gt; = sum(&lt;collection&gt;) <span class="hljs-comment"># Returns sum of elements. Also math.prod(&lt;coll&gt;).</span>
128+
<pre><code class="python language-python hljs">&lt;el&gt; = max(&lt;collection&gt;) <span class="hljs-comment"># Returns the largest element. Also min(&lt;el_1&gt;, ...).</span>
129+
&lt;num&gt; = sum(&lt;collection&gt;) <span class="hljs-comment"># Returns a sum of elements. Also math.prod(&lt;coll&gt;).</span>
130130
</code></pre>
131131
<pre><code class="python language-python hljs">elementwise_sum = [sum(pair) <span class="hljs-keyword">for</span> pair <span class="hljs-keyword">in</span> zip(list_a, list_b)]
132132
sorted_by_second = sorted(&lt;collection&gt;, key=<span class="hljs-keyword">lambda</span> el: el[<span class="hljs-number">1</span>])
@@ -143,7 +143,7 @@
143143
&lt;int&gt; = &lt;list&gt;.index(&lt;el&gt;) <span class="hljs-comment"># Returns index of the first occurrence or raises ValueError.</span>
144144
&lt;el&gt; = &lt;list&gt;.pop() <span class="hljs-comment"># Removes and returns item from the end or at index if passed.</span>
145145
&lt;list&gt;.insert(&lt;int&gt;, &lt;el&gt;) <span class="hljs-comment"># Inserts item at passed index and moves the rest to the right.</span>
146-
&lt;list&gt;.remove(&lt;el&gt;) <span class="hljs-comment"># Removes first occurrence of the item or raises ValueError.</span>
146+
&lt;list&gt;.remove(&lt;el&gt;) <span class="hljs-comment"># Removes the first occurrence or raises ValueError exception.</span>
147147
&lt;list&gt;.clear() <span class="hljs-comment"># Removes all list's items. Also works on dictionary and set.</span>
148148
</code></pre>
149149
<div><h2 id="dictionary"><a href="#dictionary" name="dictionary">#</a>Dictionary</h2><pre><code class="python language-python hljs">&lt;dict&gt; = {key_1: val_1, key_2: val_2, ...} <span class="hljs-comment"># Use `&lt;dict&gt;[key]` to get or set the value.</span>
@@ -153,14 +153,14 @@
153153
&lt;view&gt; = &lt;dict&gt;.values() <span class="hljs-comment"># Collection of values that reflects changes.</span>
154154
&lt;view&gt; = &lt;dict&gt;.items() <span class="hljs-comment"># Coll. of key-value tuples that reflects chgs.</span>
155155
</code></pre>
156-
<pre><code class="python language-python hljs">value = &lt;dict&gt;.get(key, default=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Returns default argument if key is missing.</span>
156+
<pre><code class="python language-python hljs">value = &lt;dict&gt;.get(key, default=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Returns argument default if key is missing.</span>
157157
value = &lt;dict&gt;.setdefault(key, default=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Returns and writes default if key is missing.</span>
158-
&lt;dict&gt; = collections.defaultdict(&lt;type&gt;) <span class="hljs-comment"># Returns a dict with default value `&lt;type&gt;()`.</span>
159-
&lt;dict&gt; = collections.defaultdict(<span class="hljs-keyword">lambda</span>: <span class="hljs-number">1</span>) <span class="hljs-comment"># Returns a dict with default value 1.</span>
158+
&lt;dict&gt; = collections.defaultdict(&lt;type&gt;) <span class="hljs-comment"># Dict with automatic default value `&lt;type&gt;()`.</span>
159+
&lt;dict&gt; = collections.defaultdict(<span class="hljs-keyword">lambda</span>: <span class="hljs-number">1</span>) <span class="hljs-comment"># Dictionary with automatic default value 1.</span>
160160
</code></pre>
161161
<pre><code class="python language-python hljs">&lt;dict&gt; = dict(&lt;collection&gt;) <span class="hljs-comment"># Creates a dict from coll. of key-value pairs.</span>
162-
&lt;dict&gt; = dict(zip(keys, values)) <span class="hljs-comment"># Creates a dict from two collections.</span>
163-
&lt;dict&gt; = dict.fromkeys(keys [, value]) <span class="hljs-comment"># Creates a dict from collection of keys.</span>
162+
&lt;dict&gt; = dict(zip(keys, values)) <span class="hljs-comment"># Creates a dictionary from two collections.</span>
163+
&lt;dict&gt; = dict.fromkeys(keys [, value]) <span class="hljs-comment"># Creates a dictionary from collection of keys.</span>
164164
</code></pre>
165165
<pre><code class="python language-python hljs">&lt;dict&gt;.update(&lt;dict&gt;) <span class="hljs-comment"># Adds items. Replaces ones with matching keys.</span>
166166
value = &lt;dict&gt;.pop(key) <span class="hljs-comment"># Removes item or raises KeyError if missing.</span>
@@ -174,26 +174,26 @@
174174
[(<span class="hljs-string">'blue'</span>, <span class="hljs-number">3</span>), (<span class="hljs-string">'red'</span>, <span class="hljs-number">2</span>), (<span class="hljs-string">'yellow'</span>, <span class="hljs-number">1</span>)]
175175
</code></pre></div>
176176

177-
<div><h2 id="set"><a href="#set" name="set">#</a>Set</h2><pre><code class="python language-python hljs">&lt;set&gt; = {&lt;el_1&gt;, &lt;el_2&gt;, ...} <span class="hljs-comment"># Use `set()` for empty set.</span>
177+
<div><h2 id="set"><a href="#set" name="set">#</a>Set</h2><pre><code class="python language-python hljs">&lt;set&gt; = {&lt;el_1&gt;, &lt;el_2&gt;, ...} <span class="hljs-comment"># Coll. of unique items. Also set(), set(&lt;coll&gt;).</span>
178178
</code></pre></div>
179179

180-
<pre><code class="python language-python hljs">&lt;set&gt;.add(&lt;el&gt;) <span class="hljs-comment"># Or: &lt;set&gt; |= {&lt;el&gt;}</span>
181-
&lt;set&gt;.update(&lt;collection&gt; [, ...]) <span class="hljs-comment"># Or: &lt;set&gt; |= &lt;set&gt;</span>
180+
<pre><code class="python language-python hljs">&lt;set&gt;.add(&lt;el&gt;) <span class="hljs-comment"># Adds item to the set. Also `&lt;set&gt; |= {&lt;el&gt;}`.</span>
181+
&lt;set&gt;.update(&lt;collection&gt; [, ...]) <span class="hljs-comment"># Adds items to the set. Also `&lt;set&gt; |= &lt;set&gt;`.</span>
182182
</code></pre>
183-
<pre><code class="python language-python hljs">&lt;set&gt; = &lt;set&gt;.union(&lt;coll.&gt;) <span class="hljs-comment"># Or: &lt;set&gt; | &lt;set&gt;</span>
184-
&lt;set&gt; = &lt;set&gt;.intersection(&lt;coll.&gt;) <span class="hljs-comment"># Or: &lt;set&gt; &amp; &lt;set&gt;</span>
185-
&lt;set&gt; = &lt;set&gt;.difference(&lt;coll.&gt;) <span class="hljs-comment"># Or: &lt;set&gt; - &lt;set&gt;</span>
186-
&lt;set&gt; = &lt;set&gt;.symmetric_difference(&lt;coll.&gt;) <span class="hljs-comment"># Or: &lt;set&gt; ^ &lt;set&gt;</span>
187-
&lt;bool&gt; = &lt;set&gt;.issubset(&lt;coll.&gt;) <span class="hljs-comment"># Or: &lt;set&gt; &lt;= &lt;set&gt;</span>
188-
&lt;bool&gt; = &lt;set&gt;.issuperset(&lt;coll.&gt;) <span class="hljs-comment"># Or: &lt;set&gt; &gt;= &lt;set&gt;</span>
183+
<pre><code class="python language-python hljs">&lt;set&gt; = &lt;set&gt;.union(&lt;coll&gt;) <span class="hljs-comment"># Returns a set of all items. Also &lt;set&gt; | &lt;set&gt;.</span>
184+
&lt;set&gt; = &lt;set&gt;.intersection(&lt;coll&gt;) <span class="hljs-comment"># Returns all shared items. Also &lt;set&gt; &amp; &lt;set&gt;.</span>
185+
&lt;set&gt; = &lt;set&gt;.difference(&lt;coll&gt;) <span class="hljs-comment"># Returns set's unique items. Also &lt;set&gt; - &lt;set&gt;.</span>
186+
&lt;set&gt; = &lt;set&gt;.symmetric_difference(&lt;coll&gt;) <span class="hljs-comment"># Returns non-shared items. Also &lt;set&gt; ^ &lt;set&gt;.</span>
187+
&lt;bool&gt; = &lt;set&gt;.issuperset(&lt;coll&gt;) <span class="hljs-comment"># Returns False if collection has unique items.</span>
188+
&lt;bool&gt; = &lt;set&gt;.issubset(&lt;coll&gt;) <span class="hljs-comment"># Is collection a superset? Also &lt;set&gt; &lt;= &lt;set&gt;.</span>
189189
</code></pre>
190-
<pre><code class="python language-python hljs">&lt;el&gt; = &lt;set&gt;.pop() <span class="hljs-comment"># Raises KeyError if empty.</span>
191-
&lt;set&gt;.remove(&lt;el&gt;) <span class="hljs-comment"># Raises KeyError if missing.</span>
192-
&lt;set&gt;.discard(&lt;el&gt;) <span class="hljs-comment"># Doesn't raise an error.</span>
190+
<pre><code class="python language-python hljs">&lt;el&gt; = &lt;set&gt;.pop() <span class="hljs-comment"># Removes and returns an item or raises KeyError.</span>
191+
&lt;set&gt;.remove(&lt;el&gt;) <span class="hljs-comment"># Removes the item or raises KeyError if missing.</span>
192+
&lt;set&gt;.discard(&lt;el&gt;) <span class="hljs-comment"># Same as remove() but it doesn't raise an error.</span>
193193
</code></pre>
194194
<div><h3 id="frozenset">Frozen Set</h3><ul>
195195
<li><strong>Is immutable and hashable.</strong></li>
196-
<li><strong>That means it can be used as a key in a dictionary or as an element in a set.</strong></li>
196+
<li><strong>That means it can be used as a key in a dictionary or as an item in a set.</strong></li>
197197
</ul><pre><code class="python language-python hljs">&lt;frozenset&gt; = frozenset(&lt;collection&gt;)
198198
</code></pre></div>
199199

@@ -2934,7 +2934,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29342934

29352935

29362936
<footer>
2937-
<aside>September 16, 2025</aside>
2937+
<aside>October 5, 2025</aside>
29382938
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29392939
</footer>
29402940

0 commit comments

Comments
 (0)