<span style="line-height:13px;text-align:left;color:rgb(102,102,102);font-size:13px;font-family:'Trebuchet MS',Trebuchet,Verdana,sans-serif">Sometimes you want a dict which is subset of another dict. It would nice if dict.items accepted an optional list of keys to return. If no keys are given - use default behavior - get all items.</span><br clear="all">

<div><br></div><div><pre style="color:rgb(102,102,102);font-size:13px;line-height:13px;background-color:rgb(240,240,240);border-bottom-style:none;border-color:initial;border-left-style:none;border-right-style:none;border-top-style:none;border-width:initial;font-family:'Bitstream Vera Sans Mono',monospace;padding-bottom:10px;padding-left:10px;padding-right:10px;padding-top:10px">
<span style="color:rgb(0,112,32);font-weight:bold">class</span> <span style="color:rgb(14,132,181);font-weight:bold">NewDict</span><span>(</span><span style="color:rgb(0,112,32)">dict</span><span>):</span>

    <span style="color:rgb(0,112,32);font-weight:bold">def</span> <span style="color:rgb(6,40,126)">items</span><span>(</span><span style="color:rgb(0,112,32)">self</span><span>,</span> <span>keys</span><span>=</span><span>()):</span>
        <span style="color:rgb(64,112,160);font-style:italic">"""Another version of dict.items() which accepts specific keys to use."""</span>
        <span style="color:rgb(0,112,32);font-weight:bold">for</span> <span>key</span> <span style="color:rgb(0,112,32);font-weight:bold">in</span> <span>keys</span> <span style="color:rgb(0,112,32);font-weight:bold">or</span> <span style="color:rgb(0,112,32)">self</span><span>.</span><span>keys</span><span>():</span>
            <span style="color:rgb(0,112,32);font-weight:bold">yield</span> <span>key</span><span>,</span> <span style="color:rgb(0,112,32)">self</span><span>[</span><span>key</span><span>]</span>
        
       
<span>a</span> <span>=</span> <span>NewDict</span><span>({</span>
    <span style="color:rgb(64,160,112)">1</span><span>:</span> <span style="color:rgb(64,112,160)">'one'</span><span>,</span>
    <span style="color:rgb(64,160,112)">2</span><span>:</span> <span style="color:rgb(64,112,160)">'two'</span><span>,</span>
    <span style="color:rgb(64,160,112)">3</span><span>:</span> <span style="color:rgb(64,112,160)">'three'</span><span>,</span>
    <span style="color:rgb(64,160,112)">4</span><span>:</span> <span style="color:rgb(64,112,160)">'four'</span><span>,</span>
    <span style="color:rgb(64,160,112)">5</span><span>:</span> <span style="color:rgb(64,112,160)">'five'</span>
<span>})</span>

<span style="color:rgb(0,112,32)">print</span><span>(</span><span style="color:rgb(0,112,32)">dict</span><span>(</span><span>a</span><span>.</span><span>items</span><span>()))</span>
<span style="color:rgb(0,112,32)">print</span><span>(</span><span style="color:rgb(0,112,32)">dict</span><span>(</span><span>a</span><span>.</span><span>items</span><span>((</span><span style="color:rgb(64,160,112)">1</span><span>,</span> <span style="color:rgb(64,160,112)">3</span><span>,</span> <span style="color:rgb(64,160,112)">5</span><span>))))</span></pre>

<pre style="color:rgb(102,102,102);font-size:13px;line-height:13px;background-color:rgb(240,240,240);border-bottom-style:none;border-color:initial;border-left-style:none;border-right-style:none;border-top-style:none;border-width:initial;font-family:'Bitstream Vera Sans Mono',monospace;padding-bottom:10px;padding-left:10px;padding-right:10px;padding-top:10px">
<span><pre style="border-bottom-style:none;border-color:initial;border-left-style:none;border-right-style:none;border-top-style:none;border-width:initial;font-family:'Bitstream Vera Sans Mono',monospace;padding-bottom:10px;padding-left:10px;padding-right:10px;padding-top:10px">
<span style="color:rgb(198,93,9);font-weight:bold">vic@ubuntu:~/Desktop$</span> python test.py 
<span style="color:rgb(128,128,128)">{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}</span>
<span style="color:rgb(128,128,128)">{1: 'one', 3: 'three', 5: 'five'}</span></pre></span></pre></div><div>Thanks for the attention.</div><div><br></div><div>--</div><b><i>Victor Varvariuc</i></b><br>

<br>