DefaultDict for the Network Engineer

There might come a time as a Network Engineer you will need to create a data structure to process your information. A common one I’ve had to do is creating Dictionaries. 

My_little_dictionary = { My_key, My_value}

As your use cases increase in complexity you will need to dig deeper into the python ecosystem. One such handy features is DefaultDict. It is extremely versatile, I’m only going to cover one such use case. 

So let’s decide on what our key is going to be, I started with using the hostname. I have a list that I create with the hosts. 

from collections import defaultdict
my_list = ['Host1', 'Host2', 'Host3']
d = defaultdict(list)

I’ll pause here point out the data structure for you default dictionary. 

defaultdict(list, {})

https://docs.python.org/3/library/collections.html#collections.defaultdict – “Using list as the default_factory, it is easy to group a sequence of key-value pairs into a dictionary of list.”

This simplifies dictionary lists generation, with an elegant way of calling the code, making it easy to read and follow. 

for item in my_list:
my_config = [f'hostname {item}','int gi1/0/1', ' no shut’]
d[item] = my_config

What I’m doing here is looping through my list by each item, Host1-3. I’m generating a list that calls the hostname so each list would look like – [‘hostname Host1’, ‘int gi1/0/1’, ‘ no shut’]. Then it takes that list and assigns it to the defaultdict called ‘d’. As you call each Key in the default dict, it creates an entry for the key. The variable it’s looking for is it’s list. You can insert your list into it just like you’re assigning a variable. Since it’s a for loop, it does this so on until you fill in all the hosts. The results:

defaultdict(list,
{'Host1': ['hostname Host1', 'int gi1/0/1', ' no shut'],
'Host2': ['hostname Host2', 'int gi1/0/1', ' no shut'],
'Host3': ['hostname Host3', 'int gi1/0/1', ' no shut']})

It’s a dictionary of lists. Key being the hostname and it’s values the list. Very handy when you’re needing something a little more complex and scary simple to implement. You can even pass this to pandas and have it work some magic. Running the following code generates a Table like below. 

df = pd.DataFrame.from_dict(d)
df2 = df.transpose()
df2.columns = ['Hostname', 'Interface', 'State']

UntitledImage

 

 

 

 

 

 

Hopefully this can help, if you can think of anything I should include or am wrong about? Drop me a line. I’m happy to learn.

Scroll to Top