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><a href="https://docs.python.org/3/library/functions.html#enumerate" rel="nofollow">enumerate</a> will give you the iterator index and value of the iterable. In this case the key and index of the key. Not the values of a key. And as the value is a list of string you have to check for "P" in the strings inside the list. Try like this:</p>

<pre><code>for idx, comp in components.items():
    for s in comp:
        if "P" in s:
            print(s)
</code></pre>

python dictionary