<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Loop to check dictionary object for key in Controllers</title>
    <link>https://community.cisco.com/t5/controllers/loop-to-check-dictionary-object-for-key/m-p/4633602#M1063</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't understand what you mean by '&lt;SPAN&gt;if one key is missing I need to print out the mid level key&lt;/SPAN&gt;'. But to achieve your goal -&amp;nbsp;&lt;SPAN&gt;check if dot1x has been enabled on access ports that aren't in vlan 77 - you can do the following:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;import pprint

data1 = {'interfaces': {'GigabitEthernet1/0/1': {'dot1x_pae_authenticator': True,
                                         'switchport_access_vlan': '22',
                                         'switchport_mode': 'access'},
                'GigabitEthernet1/0/2': {'switchport_access_vlan': '77',
                                         'switchport_mode': 'access'},
                'GigabitEthernet1/0/3': {'switchport_mode': 'trunk'},
                'GigabitEthernet1/0/4': {'switchport_access_vlan': '55',
                                         'switchport_mode': 'access'}}}

not_vl77_dot1x = []

for intf in data1['interfaces'].keys():
    if 'dot1x_pae_authenticator' in data1['interfaces'][intf]:
        if data1['interfaces'][intf]['dot1x_pae_authenticator'] and data1['interfaces'][intf]['switchport_mode'] == 'access':
            if data1['interfaces'][intf]['switchport_access_vlan'] != '77':
                not_vl77_dot1x.append(intf)

pprint.pprint(not_vl77_dot1x)&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;HTH&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Marcel&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 17 Jun 2022 06:26:08 GMT</pubDate>
    <dc:creator>Marcel Zehnder</dc:creator>
    <dc:date>2022-06-17T06:26:08Z</dc:date>
    <item>
      <title>Loop to check dictionary object for key</title>
      <link>https://community.cisco.com/t5/controllers/loop-to-check-dictionary-object-for-key/m-p/4633557#M1062</link>
      <description>&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;This is a python script I'm making to run against a network switch to check if do1x has been enabled on access ports that aren't in vlan 77&lt;/P&gt;
&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;I have a dictionary object that has key's nested with-in key's (don't know the correct term for this). I need to loop through this dictionary object to check for multiple conditions of keys and values but if one key is missing I need to print out the mid level key. I've provided an shortened example of the dictionary object and the code that I'm trying to get working. Any advice is greatly appreciated.&lt;/P&gt;
&lt;PRE&gt;pprint.pprint(data1)
{'interfaces': {'GigabitEthernet1/0/1': {'dot1x_pae_authenticator': True,
                                         'switchport_access_vlan': '22',
                                         'switchport_mode': 'access'},
                'GigabitEthernet1/0/2': {'switchport_access_vlan': '77',
                                         'switchport_mode': 'access'},
                'GigabitEthernet1/0/3': {'switchport_mode': 'trunk'},
                'GigabitEthernet1/0/4': {'switchport_access_vlan': '55',
                                         'switchport_mode': 'access'}}}
for x in range(1, 5):
    try:
        if data1['interfaces']['GigabitEthernet1/0/' + str(x)]['switchport_mode'] == 'access':
            if data1['interfaces']['GigabitEthernet1/0/' + str(x)]['switchport_access_vlan'] != '77':
                if data1['interfaces']['GigabitEthernet1/0/' + str(x)]['dot1x_pae_authenticator']:
                    pass
        else:
            print('int GigabitEthernet1/0/' + str(x))
    except Exception:
        pass
&lt;/PRE&gt;
&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;&lt;SPAN&gt;My goal is to get it to print int GigabitEthernet1/0/4 but instead I get int GigabitEthernet1/0/3. I don't care about gig 1/0/3 because its a trunk.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 03:19:53 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/loop-to-check-dictionary-object-for-key/m-p/4633557#M1062</guid>
      <dc:creator>AFlack20</dc:creator>
      <dc:date>2022-06-17T03:19:53Z</dc:date>
    </item>
    <item>
      <title>Re: Loop to check dictionary object for key</title>
      <link>https://community.cisco.com/t5/controllers/loop-to-check-dictionary-object-for-key/m-p/4633602#M1063</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't understand what you mean by '&lt;SPAN&gt;if one key is missing I need to print out the mid level key&lt;/SPAN&gt;'. But to achieve your goal -&amp;nbsp;&lt;SPAN&gt;check if dot1x has been enabled on access ports that aren't in vlan 77 - you can do the following:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;import pprint

data1 = {'interfaces': {'GigabitEthernet1/0/1': {'dot1x_pae_authenticator': True,
                                         'switchport_access_vlan': '22',
                                         'switchport_mode': 'access'},
                'GigabitEthernet1/0/2': {'switchport_access_vlan': '77',
                                         'switchport_mode': 'access'},
                'GigabitEthernet1/0/3': {'switchport_mode': 'trunk'},
                'GigabitEthernet1/0/4': {'switchport_access_vlan': '55',
                                         'switchport_mode': 'access'}}}

not_vl77_dot1x = []

for intf in data1['interfaces'].keys():
    if 'dot1x_pae_authenticator' in data1['interfaces'][intf]:
        if data1['interfaces'][intf]['dot1x_pae_authenticator'] and data1['interfaces'][intf]['switchport_mode'] == 'access':
            if data1['interfaces'][intf]['switchport_access_vlan'] != '77':
                not_vl77_dot1x.append(intf)

pprint.pprint(not_vl77_dot1x)&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;HTH&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Marcel&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 06:26:08 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/loop-to-check-dictionary-object-for-key/m-p/4633602#M1063</guid>
      <dc:creator>Marcel Zehnder</dc:creator>
      <dc:date>2022-06-17T06:26:08Z</dc:date>
    </item>
  </channel>
</rss>

