Printing only desired values of keys in dcitionary
<p>I have a dictionary in Python which looks like this</p>

<pre><code>{123: [u'6722000', u'6722001', u'6631999', u'PX522.X522.091003143054.S4J2', u'PXX22.XX22.140311131347.A6D4', u'7767815060', u'6631900', u'7767815062', u'18001029945', u'7767815063'],...}
</code></pre>

<p>When I want to print out the values of dictionary (or store it in a txt file), I only want the values which have "P" in it and not the one having only numbers.</p>

<p>For example, the above dictionary should look like  </p>

<pre><code>{123: [u'PX522.X522.091003143054.S4J2', u'PXX22.XX22.140311131347.A6D4'],...}
</code></pre>

<p>This is the code that I have written </p>

<pre><code>components = nx.connected_component_subgraphs(G)

for idx, comp in enumerate(components):
    if "P" in comp.nodes():
       comp_dict.update({idx: comp.nodes()})
</code></pre>

<p>But it's giving me no output.
If I execute the code without the if-statement, I'm getting the same output with all the values.</p>

<p><strong>Code with comments inline:</strong></p>

<pre><code>a_dict = {123: [u'6722000', u'6722001', u'6631999', u'PX522.X522.091003143054.S4J2', u'PXX22.XX22.140311131347.A6D4', u'7767815060', u'6631900', u'7767815062', u'18001029945', u'7767815063'],
               456: [u'6722000', u'6722001', u'6631999', u'PX522.X522.091003143054.S4J2']}
    print "Before...",a_dict
    #Iterate over keys of dictonary
    for k in a_dict.keys():
        #Check the values in list for 'P'. Keep only which has 'P'
        tmp_list = [i for i in a_dict[k] if "P" in i]
        #Update dictionary with new list
        a_dict[k]=tmp_list
        tmp_list = []
    print "After...", a_dict
</code></pre>

<p><strong>Output:</strong></p>

<pre><code>C:\Users\dinesh_pundkar\Desktop&gt;python c.py
Before... {456: [u'6722000', u'6722001', u'6631999', u'PX522.X522.091003143054.S
4J2'], 123: [u'6722000', u'6722001', u'6631999', u'PX522.X522.091003143054.S4J2'
, u'PXX22.XX22.140311131347.A6D4', u'7767815060', u'6631900', u'7767815062', u'1
8001029945', u'7767815063']}
After... {456: [u'PX522.X522.091003143054.S4J2'], 123: [u'PX522.X522.09100314305
4.S4J2', u'PXX22.XX22.140311131347.A6D4']}

C:\Users\dinesh_pundkar\Desktop&gt;
</code></pre>

<p>Please let me know if you have query.</p>

python dictionary