I want to share a recent addition to my network automation toolbox - Dq (Dictionary query).

Dq is a Cisco Genie library that provides various helper functions to ease the parsing of nested Python dictionaries.

For example, let's say we have the following Python dictionary.

>>> print(ospf_neighbors)
'ipv4': {
    'instance': {
        '1': {
            'areas': {
                '': {
                    'interfaces': {
                        'Ethernet1/1': {
                            'neighbors': {
                                '': {
                                    'router_id': '',
                                    'address': '',
                                    'state': 'full',
                                    'last_state_change': '1d22h',

Normally if we wanted to pull out the state for the neighbour we would need to perform a query much like the below (painful right?).

>>> ospf_neighbors['vrf']['default']['address_family']['ipv4']['instance']['1']['areas']['']['interfaces']['Ethernet1/1']['neighbors']['']['state']

With Dq, we can simply perform the following! Niiiice!

$ pip3 install "pyats[library]"
>>> from genie.utils import Dq
>>> Dq(ospf_neighbors).contains('').get_values('state', 0)
Ready to Master Network Automation? Start Your Journey Today!
Our membership provides:
  • Full deep-dive course library (inc. Batfish, pyATS, Netmiko)
  • Code repositories inc. full course code, scripts and examples
  • 24x7 multi-vendor labs (Arista, Cisco, Juniper)
  • Private online community
  • Live monthly tech sessions
  • Access to tech session library

Join Now ➜