Like python lists and python tuples, we can access a specific item in a python dictionary. To do this, we use both index method and get method. Beside, we can also access the complete keys and values in a dictionary. Let’s show these methods with different Python coding examples.
You can check also complete Python Dictionary Lessons
Firstly, let’s see how to access a value of a key:value pair.
The output of this python code will be:
You can learn all Python Dictionary Methods
Here, we have used index operator. We add the key inside square brackets and access the value of this key. We can get the same result with get() method.
We can use these methods in networking liek below:
These are the methods used to access any value of a key:value pair. Now, let’s see how to access both all the keys and all the values in a python dictionary.
You can learn all How to add a Python Dictionary
Below, we will access a all keys of a character dictionary.
The output of this code will be:
As you can see above, all the keys are listed. Below, we will access the keys of device dictionary.
Now, let’s access the values of a character and device dictionaries.
The second networking example will be like below:
We can access the comlete items in a python dictionary with the help of items function. The return of this fucntion will be as tuple in a list.
Below, we will access the items of the dictionaries and as an output, we will receive an a tuple in a list.
In this lesson, we have learned how to access the keys and values of a python dictionary. With diffferent exaamples, you can increase your experience on Python Dictionary access methods.
Leave a Reply