Py for Network Engineers: Day 6 Dict and sets
Dicts are unordered mutable for value field {key : value, key : value}. Values access using key names

Py for Network Engineers: Day 6 Dict and sets
Dicts are unordered mutable for value field {key : value, key : value}. Values access using key names
In [1]: my_dict = { "name": "rafael", "age": 35, "name": "silva", "hobbies": "Drums"
...: }
In [2]: my_dict
Out[2]: {'name': 'silva', 'age': 35, 'hobbies': 'Drums'}
In [3]: people = [{"name": "rafael", "age": 35, "hobbies": "Drums"}, {"name": "Laure
...: n", "age": 20, "hobbies": "IT"}]
In [4]: people
Out[4]:
[{'name': 'rafael', 'age': 35, 'hobbies': 'Drums'},
{'name': 'Lauren', 'age': 20, 'hobbies': 'IT'}]
In [5]: people[0]
Out[5]: {'name': 'rafael', 'age': 35, 'hobbies': 'Drums'}
In [6]: people[1]
Out[6]: {'name': 'Lauren', 'age': 20, 'hobbies': 'IT'}
In [7]: people[1][0]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[7], line 1
----> 1 people[1][0]
KeyError: 0
In [8]: people[1]["name"]
Out[8]: 'Lauren'
Adding key and accessing values
In [1]: device = {"hostname": "192.168.1.1", "username": "cisco", "password":"cisco"}
In [2]: device
Out[2]: {'hostname': '192.168.1.1', 'username': 'cisco', 'password': 'cisco'}
In [3]: type(device)
Out[3]: dict
In [4]: id(device)
Out[4]: 4456655104
In [5]: device["platform"] = "cisco_xe"
In [6]: device
Out[6]:
{'hostname': '192.168.1.1',
'username': 'cisco',
'password': 'cisco',
'platform': 'cisco_xe'}
In [7]: id(device)
Out[7]: 4456655104
In [8]: device["hostname"]
Out[8]: '192.168.1.1'
In [9]: people = [{"name": "rafael", "age": 35, "hobbies": "Drums"}, {"name": "Lauren", "age": 20, "hobbies"
...: : "IT"}]
In [10]: type(people)
Out[10]: list
In [11]: people[1]["age"]
Out[11]: 20
In [12]: device
Out[12]:
{'hostname': '192.168.1.1',
'username': 'cisco',
'password': 'cisco',
'platform': 'cisco_xe'}
In [13]: device["version"]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[13], line 1
----> 1 device["version"]
KeyError: 'version'
# Method get to avoid err
In [14]: device.get('version')
In [15]: device.get('platform')
Out[15]: 'cisco_xe'
In [16]: device.get('version', "This key cannot be found")
Out[16]: 'This key cannot be found'
In [17]: device.get('platdorm', "This key cannot be found")
Out[17]: 'This key cannot be found'
In [18]: device.get('platform', "This key cannot be found")
Out[18]: 'cisco_xe'
In [19]:
Removing keys
Using del and Pop
In [20]: device
Out[20]:
{'hostname': '192.168.1.1',
'username': 'cisco',
'password': 'cisco',
'platform': 'cisco_xe'}
In [21]: del device["password"]
In [22]: device
Out[22]: {'hostname': '192.168.1.1', 'username': 'cisco', 'platform': 'cisco_xe'}
In [23]: del device["version"]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[23], line 1
----> 1 del device["version"]
KeyError: 'version'
In [24]: del device["password"]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[24], line 1
----> 1 del device["password"]
KeyError: 'password'
In [25]: device["password"] = "cisco"
In [26]: device
Out[26]:
{'hostname': '192.168.1.1',
'username': 'cisco',
'platform': 'cisco_xe',
'password': 'cisco'}
In [27]: device.pop()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[27], line 1
----> 1 device.pop()
TypeError: pop expected at least 1 argument, got 0
In [28]: device.pop("password")
Out[28]: 'cisco'
In [29]: device
Out[29]: {'hostname': '192.168.1.1', 'username': 'cisco', 'platform': 'cisco_xe'}
In [30]: device.pop("password")
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[30], line 1
----> 1 device.pop("password")
KeyError: 'password'
In [31]: deleted_partform = device.pop("platform")
In [32]: device
Out[32]: {'hostname': '192.168.1.1', 'username': 'cisco'}
In [33]: deleted_partform
Out[33]: 'cisco_xe'
In [34]: device.pop("platform")
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[34], line 1
----> 1 device.pop("platform")
KeyError: 'platform'
In [35]: device.pop("platform", "Key does not exist!")
Out[35]: 'Key does not exist!'
In [36]:
Key, values and items
In [4]: device_info = {"layer": "distribuition", "ASN": 65001, "platform": "cisco_xe", "version": 15.6, "ser
...: ial_number": "AKSOAKSPOAKSOPK"}
In [5]: device_info
Out[5]:
{'layer': 'distribuition',
'ASN': 65001,
'platform': 'cisco_xe',
'version': 15.6,
'serial_number': 'AKSOAKSPOAKSOPK'}
In [6]: device_info.keys()
Out[6]: dict_keys(['layer', 'ASN', 'platform', 'version', 'serial_number'])
In [7]: type(device_info.keys())
Out[7]: dict_keys
In [8]: list_of_keys = list(device_info.keys())
In [9]: list_of_keys
Out[9]: ['layer', 'ASN', 'platform', 'version', 'serial_number']
In [10]: type(list_of_keys)
Out[10]: list
In [11]: list_of_keys[0]
Out[11]: 'layer'
In [12]: list_of_keys[1]
Out[12]: 'ASN'
In [13]: list_of_keys[2]
Out[13]: 'platform'
In [14]: device_info
Out[14]:
{'layer': 'distribuition',
'ASN': 65001,
'platform': 'cisco_xe',
'version': 15.6,
'serial_number': 'AKSOAKSPOAKSOPK'}
In [15]: device_info.values()
Out[15]: dict_values(['distribuition', 65001, 'cisco_xe', 15.6, 'AKSOAKSPOAKSOPK'])
In [16]: type(device_info.values())
Out[16]: dict_values
In [18]: list_of_values = list(device_info.values())
In [19]: list_of_values
Out[19]: ['distribuition', 65001, 'cisco_xe', 15.6, 'AKSOAKSPOAKSOPK']
In [20]: type(list_of_values)
Out[20]: list
In [21]: list_of_values[0]
Out[21]: 'distribuition'
In [22]: list_of_values[1]
Out[22]: 65001
In [23]: list_of_values[2]
Out[23]: 'cisco_xe'
In [24]: device_info
Out[24]:
{'layer': 'distribuition',
'ASN': 65001,
'platform': 'cisco_xe',
'version': 15.6,
'serial_number': 'AKSOAKSPOAKSOPK'}
In [25]:
In [25]: device_info.items()
Out[25]: dict_items([('layer', 'distribuition'), ('ASN', 65001), ('platform', 'cisco_xe'), ('version', 15.6), ('serial_number', 'AKSOAKSPOAKSOPK')])
In [27]: type(device_info.items())
Out[27]: dict_items
In [28]: list_of_items = list(device_info.items())
In [29]: type(list_of_items)
Out[29]: list
In [30]: list_of_items
Out[30]:
[('layer', 'distribuition'),
('ASN', 65001),
('platform', 'cisco_xe'),
('version', 15.6),
('serial_number', 'AKSOAKSPOAKSOPK')]
In [31]: list_of_items[0]
Out[31]: ('layer', 'distribuition')
In [32]: list_of_items[1]
Out[32]: ('ASN', 65001)
In [33]: list_of_items[2]
Out[33]: ('platform', 'cisco_xe')
In [34]: list_of_items[3]
Out[34]: ('version', 15.6)
In [35]: list_of_items[4]
Out[35]: ('serial_number', 'AKSOAKSPOAKSOPK')
In [36]: type(list_of_items[4])
Out[36]: tuple
In [37]: list_of_items[4][1]
Out[37]: 'AKSOAKSPOAKSOPK'
In [38]:
Iterating over dictionaries
In [1]: device_info = { "device_type": "router", "model": "XR", "serial_number": "OAKSPOKAOKSOPKAS", "Protoc
...: ol": "OSPF", "Location": "USA"}
In [2]: device_info
Out[2]:
{'device_type': 'router',
'model': 'XR',
'serial_number': 'OAKSPOKAOKSOPKAS',
'Protocol': 'OSPF',
'Location': 'USA'}
In [3]: for key in device_info:
...: print(key)
...:
device_type
model
serial_number
Protocol
Location
In [4]: for key in device_info:
...: print(f"Device info dict has a key called {key}")
...:
Device info dict has a key called device_type
Device info dict has a key called model
Device info dict has a key called serial_number
Device info dict has a key called Protocol
Device info dict has a key called Location
In [6]: for value in device_info.values():
...: print(value)
...:
router
XR
OAKSPOKAOKSOPKAS
OSPF
USA
In [8]: for value in device_info.values():
...: print(f"The device_info dect has a value called {value}")
...:
The device_info dect has a value called router
The device_info dect has a value called XR
The device_info dect has a value called OAKSPOKAOKSOPKAS
The device_info dect has a value called OSPF
The device_info dect has a value called USA
In [9]: device_info.items()
Out[9]: dict_items([('device_type', 'router'), ('model', 'XR'), ('serial_number', 'OAKSPOKAOKSOPKAS'), ('Protocol', 'OSPF'), ('Location', 'USA')])
In [10]: for item in device_info.items():
...: print(item)
...:
('device_type', 'router')
('model', 'XR')
('serial_number', 'OAKSPOKAOKSOPKAS')
('Protocol', 'OSPF')
('Location', 'USA')
In [11]: for k, v in device_info.items():
...: print(k)
...:
device_type
model
serial_number
Protocol
Location
In [12]: for k, v in device_info.items():
...: print(v)
...:
router
XR
OAKSPOKAOKSOPKAS
OSPF
USA
In [13]: for k, v in device_info.items():
...: print(f"The device_info dict has a key called {k} with a value of {v}")
...:
The device_info dict has a key called device_type with a value of router
The device_info dict has a key called model with a value of XR
The device_info dict has a key called serial_number with a value of OAKSPOKAOKSOPKAS
The device_info dict has a key called Protocol with a value of OSPF
The device_info dict has a key called Location with a value of USA
In [14]:
In [14]: for key in sorted(device_info.keys()):
...: print(key)
...:
Location
Protocol
device_type
model
serial_number
In [15]:
Unpacking dictionaries
n [18]: chocolate_prices = {"dairy milk": 70, "mars": 65, "galaxy": 75}
In [19]: chocolate_prices
Out[19]: {'dairy milk': 70, 'mars': 65, 'galaxy': 75}
In [20]: chocolate_prices['mars']
Out[20]: 65
In [21]: pizza_prices = {"pepperoni": 7, "spicy chicken": 8}
In [22]: chocolate_prices
Out[22]: {'dairy milk': 70, 'mars': 65, 'galaxy': 75}
In [23]: pizza_prices
Out[23]: {'pepperoni': 7, 'spicy chicken': 8}
In [24]: food_prices = { **chocolate_prices, **pizza_prices}
In [25]: food_prices
Out[25]:
{'dairy milk': 70,
'mars': 65,
'galaxy': 75,
'pepperoni': 7,
'spicy chicken': 8}
In [26]:
Pratical unpack dict accessing always-on Devnet Sandbox
#!/usr/bin/env python3
from scrapli.driver.core import IOSXEDriver
MY_DICT = {
"host": "devnetsandboxiosxec8k.cisco.com",
"auth_username": "YYYYYYY",
"auth_password": "XXXXXX",
"auth_strict_key": False,
}
with IOSXEDriver(**MY_DICT) as conn:
result = conn.send_command("show version")
print(result.result)
SETS
- Only unique values are allowed inside sets.
- Mutable
In [27]: my_ip_addess = ["192.168.1.1", "192.168.2.1", "8.8.8.8"]
In [28]: my_ip_addess[0]
Out[28]: '192.168.1.1'
In [29]: my_ip_addess[0] = "192.168.1.2"
In [30]: my_ip_addess[0]
Out[30]: '192.168.1.2'
Pop removes randomly items from sets
In [32]: my_set = { "cat", "dog", "lion", "tiger", "zebra"}
In [33]: my_set
Out[33]: {'cat', 'dog', 'lion', 'tiger', 'zebra'}
In [34]: id(my_set)
Out[34]: 4573830048
In [36]: my_set.add("kangaroo")
In [37]: id(my_set)
Out[37]: 4573830048
In [38]:
In [38]:
In [38]: my_set.remove("dog")
In [39]: my_set.pop("lion")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[39], line 1
----> 1 my_set.pop("lion")
TypeError: set.pop() takes no arguments (1 given)
In [40]: my_set.pop()
Out[40]: 'kangaroo'
In [41]:
In [41]: my_set
Out[41]: {'cat', 'lion', 'tiger', 'zebra'}
In [42]: my_set.pop()
Out[42]: 'lion'
In [43]: my_set.pop()
Out[43]: 'zebra'
In [44]: my_set.add("chetaah")
In [45]: removed_animal = my_set.pop()
In [46]: removed_animal
Out[46]: 'cat'
Set operations & methods
In [48]: dev1_vrfs = {"mgmt", "customerA", "customerB"}
In [49]: dev3_vrfs = {"mgmt", "customerA", "customerC"}
In [51]: dev1_vrfs - dev3_vrfs
Out[51]: {'customerB'}
In [52]: dev3_vrfs - dev1_vrfs
Out[52]: {'customerC'}
In [53]: dev3_vrfs.difference(dev1_vrfs)
Out[53]: {'customerC'}
In [54]: dev1_vrfs = { "customerA", "customerB", "mgmt"}
In [55]: dev2_vrfs = { "customerC","mgmt", "customerB"}
In [56]: dev1_vrfs - dev2_vrfs
Out[56]: {'customerA'}
In [57]: dev2_vrfs - dev1_vrfs
Out[57]: {'customerC'}
In [58]: dev1_vrfs.union(dev2_vrfs)
Out[58]: {'customerA', 'customerB', 'customerC', 'mgmt'}
In [59]: dev2_vrfs.intersection(dev1_vrfs)
Out[59]: {'customerB', 'mgmt'}
In [60]:
$ WHOAMI

What's Next
TBD
메타데이터
- post_id
- 4186bbb8df8d
- slug
- py-for-network-engineers-day-6-dict-and-sets-4186bbb8df8d
- url
- https://medium.com/@rafaesil/py-for-network-engineers-day-6-dict-and-sets-4186bbb8df8d
- canonical_url
- https://medium.com/@rafaesil/py-for-network-engineers-day-6-dict-and-sets-4186bbb8df8d
- author_url
- https://medium.com/@rafaesil
- status
- ok
- fetched_at
- 2026-07-13 06:23:13