Search a List of Dictionaries in Python

Preface

I’m a Network Engineer learning Python, and these are purely my notes. I’m not an expert by any means. Feel free to use any of these examples and improve upon them. Feel free to call me out where things can be done better.

Onward

This post is sort of a follow-up to my last one regarding CSVs converted to dictionaries. So, I have this big list of dictionaries, these key/value pairs, and I want to do things with them. In this case, I’d like to be able to search this list and return just the key/values for a specific device.  This will later be used when building network device configuration files.

Using the last post as reference, I’ll quickly hop onto the Python interpreter, open my CSV and read it in as a list of dictionaries

>>> import csv
>>> reader = csv.DictReader(open('device-specifics.csv', 'rb'))
>>> dict_list = []
>>> 
>>> for line in reader:
...     dict_list.append(line)
... 
>>> 
>>> import pprint
>>> pprint.pprint(dict_list)
[{'$STR_CRYPTO_KEY': '06Tcqh7WEDeYMDz7F8iA',
  '$STR_LOOP': '10.255.192.1',
  '$STR_NET': '10.163.1',
  '$STR_NUM': '1',
  '$TUN_IP': '10.255.140.2'},
 {'$STR_CRYPTO_KEY': 'CS7sXm5LWDyZE554l4Ga',
  '$STR_LOOP': '10.255.192.2',
  '$STR_NET': '10.163.2',
  '$STR_NUM': '2',
  '$TUN_IP': '10.255.140.3'},
 {'$STR_CRYPTO_KEY': 'YdyQ85ThUvUgssxaUc2l',
  '$STR_LOOP': '10.255.192.3',
  '$STR_NET': '10.163.3',
  '$STR_NUM': '3',
  '$TUN_IP': '10.255.140.4'},
 {'$STR_CRYPTO_KEY': 'orZG0TKrqy098FilZaT2',
  '$STR_LOOP': '10.255.192.4',
  '$STR_NET': '10.163.4',
  '$STR_NUM': '4',
  '$TUN_IP': '10.255.140.5'},
 {'$STR_CRYPTO_KEY': 'sAvyto9yhXWZm41mijt8',
  '$STR_LOOP': '10.255.192.5',
  '$STR_NET': '10.163.5',
  '$STR_NUM': '5',
  '$TUN_IP': '10.255.140.6'},
 {'$STR_CRYPTO_KEY': '1xQVsXc94T5b0miSydg2',
  '$STR_LOOP': '10.255.192.6',
  '$STR_NET': '10.163.6',
  '$STR_NUM': '6',
  '$TUN_IP': '10.255.140.7'}]
>>> 

We can iterate over this list and print each dictionary

>>> for line in dict_list:
...     print line
... 
{'$STR_CRYPTO_KEY': '06Tcqh7WEDeYMDz7F8iA', '$TUN_IP': '10.255.140.2', '$STR_NUM': '1', '$STR_NET': '10.163.1', '$STR_LOOP': '10.255.192.1'}
{'$STR_CRYPTO_KEY': 'CS7sXm5LWDyZE554l4Ga', '$TUN_IP': '10.255.140.3', '$STR_NUM': '2', '$STR_NET': '10.163.2', '$STR_LOOP': '10.255.192.2'}
{'$STR_CRYPTO_KEY': 'YdyQ85ThUvUgssxaUc2l', '$TUN_IP': '10.255.140.4', '$STR_NUM': '3', '$STR_NET': '10.163.3', '$STR_LOOP': '10.255.192.3'}
{'$STR_CRYPTO_KEY': 'orZG0TKrqy098FilZaT2', '$TUN_IP': '10.255.140.5', '$STR_NUM': '4', '$STR_NET': '10.163.4', '$STR_LOOP': '10.255.192.4'}
{'$STR_CRYPTO_KEY': 'sAvyto9yhXWZm41mijt8', '$TUN_IP': '10.255.140.6', '$STR_NUM': '5', '$STR_NET': '10.163.5', '$STR_LOOP': '10.255.192.5'}
{'$STR_CRYPTO_KEY': '1xQVsXc94T5b0miSydg2', '$TUN_IP': '10.255.140.7', '$STR_NUM': '6', '$STR_NET': '10.163.6', '$STR_LOOP': '10.255.192.6'}
>>> 

Say I wanted just the values for Store 4 (‘$STR_NUM’: ‘4’). One way to do this is iterate through the list and search for it.

>>> for line in dict_list:
...     if line['$STR_NUM'] == '4':
...         print line
... 
{'$STR_CRYPTO_KEY': 'orZG0TKrqy098FilZaT2', '$TUN_IP': '10.255.140.5', '$STR_NUM': '4', '$STR_NET': '10.163.4', '$STR_LOOP': '10.255.192.4'}

 

Notice that got me just the single line I was looking for.

In a function

Realistically I’d want to return the values to use later in the script, not just simply print them to the screen. No problem, we’ll turn it into a basic function. This function will take in two arguments: a will equal the Store Number ($STR_NUM) and b will equal the dictionary list.

>>> def search_dict_list(a, b):
...     for line in b:
...         if line['$STR_NUM'] == a:
...             return line
... 
>>> 

Now we can call the function, sending store number ‘4’ and our dict_list as the arguments

>>> search_dict_list('4', dict_list)
{'$STR_CRYPTO_KEY': 'orZG0TKrqy098FilZaT2', '$TUN_IP': '10.255.140.5', '$STR_NUM': '4', '$STR_NET': '10.163.4', '$STR_LOOP': '10.255.192.4'}

Results! Search for another…

>>> search_dict_list('2', dict_list)
{'$STR_CRYPTO_KEY': 'CS7sXm5LWDyZE554l4Ga', '$TUN_IP': '10.255.140.3', '$STR_NUM': '2', '$STR_NET': '10.163.2', '$STR_LOOP': '10.255.192.2'}

David Varnum

here

You may also like...

1 Response

  1. Z A says:

    Thanks for sharing this page, this is very useful for Network Engineers doing Network programmbility

Leave a Reply

Discover more from /overlaid

Subscribe now to keep reading and get access to the full archive.

Continue reading