Lab 07 - Working with Dictionaries¶
This lab explores dictionary syntax and how to modify dictionary content.
- Lab 07 - Working with Dictionaries
- Task 1 - Explore Dictionary Syntax
- Task 2 - Update and Modify Dictionary Contents
Task 1 - Explore Dictionary Syntax¶
Step 1-1¶
Create a variable called facts and assign it the value of {} that will make it an empty dictionary:
Step 1-2¶
Perform a type check on facts to prove it's a dictionary:
Step 1-3¶
Add a single key-value pair to the facts dictionary. The key should be 'vendor' and the value should be 'cisco':
Step 1-4¶
Print the dictionary after adding the key-value pair:
Step 1-5¶
Perform a len() check on facts:
The dictionary facts has a length of 1 because there is one "item" in the dictionary, that is ONE key-value pair.
Step 1-6¶
Add a few more key-value pairs as shown below and when done, print the updated facts dictionary:
>>> facts['os'] = 'nxos'
>>> facts['version'] = '7.1'
>>> facts['platform'] = 'nexus'
>>>
>>> print(facts)
{'os': 'nxos', 'version': '7.1', 'vendor': 'cisco', 'platform': 'nexus'}
>>>
Step 1-7¶
Update the version from "7.1" to "7.3" and print the dictionary to verify it:
>>> facts['version'] = "7.3"
>>>
>>> facts
{'os': 'nxos', 'version': '7.3', 'vendor': 'cisco', 'platform': 'nexus'}
>>>
Step 1-8¶
You can also create a dictionary using the same notation you see when you print it. In the first few steps, you gradually added key-value pairs. However, you could just assign a new dictionary a value when it's created too:
Step 1-9¶
Another option is to use the built-in dict() function when creating dictionaries:
>>>
>>> facts_3 = dict(hostname='APAC1', vendor='arista', location='Sydney', model='7050')
>>> facts_3
{'hostname': 'APAC1', 'vendor': 'arista', 'location': 'Sydney', 'model': '7050'}
When you print it, it looks the exact same as those created by previous methods.
Step 1-10¶
Using this same syntax, you can also create an empty dictionary:
Task 2 - Update and Modify Dictionary Contents¶
Step 2-1¶
Review the built-in methods for dictionaries using the dir() function on facts:
>>> dir(facts)
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
Step 2-2¶
Print the keys for facts:
Step 2-3¶
Print the values for facts:
Step 2-4¶
Now print the keys and the values for facts_2:
>>> print(facts_2.keys())
dict_keys(['os', 'version', 'vendor', 'platform'])
>>>
>>> print(facts_2.values())
dict_values(['ios', '16.6', 'cisco', 'catalyst'])
>>>
Step 2-5¶
Print the value for hostname in facts_3:
Step 2-6¶
Print the value for the key called os in facts:
Step 2-7¶
Try to print the value for a key called os_version in facts:
>>> print(facts['os_version'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'os_version'
>>>
As expected, an error is thrown since the key DOES NOT exist.
Step 2-8¶
Now use the get method to try and return the value assigned to the os_version key:
Notice there is no error - it's a bit cleaner to use.
Step 2-9¶
Not only is get cleaner, but you can return a designated value if the desired key doesn't exist:
Step 2-10¶
Repeat the same step, but do not return "ERROR":
This is common when you just want to check to see if the key has any value assigned to it. We haven't covered conditionals yet, but we would now be able to do if os_ver: and it would return True if there is any non null value assigned to it. This is what we alluded to at the end of the Booleans Lab.
Using get is a safe approach that allows you to check to see if there is a value assigned without knowing if the key exists (and without needing try/except error handling).
Let's continue looking at other methods.
Step 2-11¶
Remove the hostname key-value pair from facts_3 using the pop method.
Remember to print the dictionary as much you want in between modifications, to see how the dictionary is being modified by the methods you are executing.
>>> facts_3
{'hostname': 'APAC1', 'vendor': 'arista', 'location': 'Sydney', 'model': '7050'}
>>>
>>> facts_3.pop('hostname')
'APAC1'
>>>
>>> facts_3
{'vendor': 'arista', 'location': 'Sydney', 'model': '7050'}
Step 2-12¶
Update the hostname of facts_3 to be nycr01:
>>> facts_3['hostname'] = 'nycr01'
>>>
>>> facts_3
{'model': '7050', 'hostname': 'nycr01', 'vendor': 'arista', 'location': 'Sydney'}
>>>
Step 2-13¶
Create a new dictionary called static_facts. It should have two key value pairs.
- customer: acme
- device_type: switch
Step 2-14¶
The facts in the static_facts dictionary are pertinent for all 3 other devices that already have their own facts dictionaries: facts, facts_2, facts_3.
Use the update method to combine static_facts with all 3 other dictionaries.
>>> facts.update(static_facts)
>>>
>>> facts_2.update(static_facts)
>>>
>>> facts_3.update(static_facts)
>>>
Verify the updated dictionaries:
>>> facts
{'vendor': 'cisco', 'os': 'nxos', 'version': '7.3', 'platform': 'nexus', 'customer': 'acme', 'device_type': 'switch'}
>>>
>>> facts_2
{'os': 'ios', 'version': '16.6', 'vendor': 'cisco', 'platform': 'catalyst', 'customer': 'acme', 'device_type': 'switch'}
>>>
>>> facts_3
{'vendor': 'arista', 'location': 'Sydney', 'model': '7050', 'hostname': 'nycr01', 'customer': 'acme', 'device_type': 'switch'}
>>>