Skip to content

Commit 7db86ad

Browse files
committed
Dict
1 parent 9cfde48 commit 7db86ad

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,16 @@ value = <dict>.setdefault(key, default=None) # Returns and writes default if
8080
```
8181

8282
```python
83-
<dict>.update(<dict>)
8483
<dict> = dict(<collection>) # Creates a dict from coll. of key-value pairs.
8584
<dict> = dict(zip(keys, values)) # Creates a dict from two collections.
8685
<dict> = dict.fromkeys(keys [, value]) # Creates a dict from collection of keys.
8786
```
8887

8988
```python
9089
value = <dict>.pop(key) # Removes item or raises KeyError.
91-
{k: v for k, v in <dict>.items() if k in keys} # Filters dictionary by keys.
90+
<dict>.update(<dict>) # Adds items. Replaces ones with matching keys.
91+
[k for k, v in <dict>.items() if v == value] # Returns list of keys that point to value.
92+
{k: v for k, v in <dict>.items() if k in keys} # Returns dictionary filtered by keys.
9293
```
9394

9495
### Counter

index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,14 @@
266266
&lt;dict&gt; = collections.defaultdict(&lt;type&gt;) <span class="hljs-comment"># Creates a dict with default value of type.</span>
267267
&lt;dict&gt; = collections.defaultdict(<span class="hljs-keyword">lambda</span>: <span class="hljs-number">1</span>) <span class="hljs-comment"># Creates a dict with default value 1.</span>
268268
</code></pre>
269-
<pre><code class="python language-python hljs">&lt;dict&gt;.update(&lt;dict&gt;)
270-
&lt;dict&gt; = dict(&lt;collection&gt;) <span class="hljs-comment"># Creates a dict from coll. of key-value pairs.</span>
269+
<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>
271270
&lt;dict&gt; = dict(zip(keys, values)) <span class="hljs-comment"># Creates a dict from two collections.</span>
272271
&lt;dict&gt; = dict.fromkeys(keys [, value]) <span class="hljs-comment"># Creates a dict from collection of keys.</span>
273272
</code></pre>
274273
<pre><code class="python language-python hljs">value = &lt;dict&gt;.pop(key) <span class="hljs-comment"># Removes item or raises KeyError.</span>
275-
{k: v <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> &lt;dict&gt;.items() <span class="hljs-keyword">if</span> k <span class="hljs-keyword">in</span> keys} <span class="hljs-comment"># Filters dictionary by keys.</span>
274+
&lt;dict&gt;.update(&lt;dict&gt;) <span class="hljs-comment"># Adds items. Replaces ones with matching keys.</span>
275+
[k <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> &lt;dict&gt;.items() <span class="hljs-keyword">if</span> v == value] <span class="hljs-comment"># Returns list of keys that point to value.</span>
276+
{k: v <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> &lt;dict&gt;.items() <span class="hljs-keyword">if</span> k <span class="hljs-keyword">in</span> keys} <span class="hljs-comment"># Returns dictionary filtered by keys.</span>
276277
</code></pre>
277278
<div><h3 id="counter">Counter</h3><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> Counter
278279
<span class="hljs-meta">&gt;&gt;&gt; </span>colors = [<span class="hljs-string">'blue'</span>, <span class="hljs-string">'red'</span>, <span class="hljs-string">'blue'</span>, <span class="hljs-string">'red'</span>, <span class="hljs-string">'blue'</span>]

0 commit comments

Comments
 (0)