Adding & Removing Keys
Following is the syntax to add new Keys to a Dictionary
Removing can be done with 2 ways each way has its own use case. 1st one is using del keyword which will just remove the key with it's value.
Second method is to use pop() function. Since this method returns a value, they can be assigned to a variable to store the deleted values for later reference if needed.
Keys, Values & Items Functions
Using keys() function against a Dictionaries will get all keys out and can be assigned to a List like the following.
values() function do the same thing for values..
items() function can be used to get all key-value pairs as Tuples inside a List..
Iterating over Dictionaries
We can run loops over through the keys and values or both in a Dictionary using the above mentioned functions just like in Lists and they are very handy in Network Engineering related work.
Following example shows how items() function is used to opt out all keys and values in a Dictionary.
Unpacking Dictionaries
You can merge 2 or more Dictionaries in to one Dictionary by this.
But you can't do this if the keys are duplicated. It will replace the values.
Nesting Dictionaries in Lists
Dictionaries can be nested inside Lists. Since the Keys cannot be duplicated, we can use 2 or more Dictionaries inside Lists to store data.
No comments:
Post a Comment