Python Dictionary Access

python-dictionary-access-.pcisco-1

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.

 

Firstly, let’s see how to access  a value of a key:value pair.

 

character =        {
  "race": "wizard",
  "name": "Gandalf",
  "color": "grey"
}
print(character["name"])

 

The output of this python code will be:

 

Gandalf

 

python-dictionary-access-.pcisco-1

 

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.

 

character =        {
  "race": "wizard",
  "name": "Gandalf",
  "color": "grey"
}
print(character.get("name"))

 

Gandalf

python-dictionary-access-.pcisco-2

 

We can use these methods in networking liek below:

 

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
}
print(device["model"])

 

9000 series

 

python-dictionary-access-.pcisco-3

 

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
}
print(device.get("model"))
9000 series

 

python-dictionary-access-items-4

 

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.

 


 

Accessing Keys and Values

 

Below, we will access  a all keys of a character dictionary.

 

character =        {
  "race": "wizard",
  "name": "Gandalf",
  "color": "grey"
}
print(character.keys())

 

The output of this code will be:

 

dict_keys(['race', 'name', 'color'])

 

how-to-access-dictionary-items-1

 

As you can see above, all the keys are listed. Below, we will access the keys of device dictionary.

 

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
}
print(device.keys())
dict_keys(['vendor', 'model', 'RU'])

 

 

 Now, let’s access the values of a character and device dictionaries.

 

character =        {
  "race": "wizard",
  "name": "Gandalf",
  "color": "grey"
}
print(character.values())

 

dict_values(['wizard', 'Gandalf', 'grey'])

how-to-access-dictionary-items-2

 

The second networking example will be like below:

 

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
}
print(device.values())

 

dict_values(['Cisco', '9000 series', 44])

 


 

Accessing Complete Items

 

 

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.

 

character =        {
  "race": "wizard",
  "name": "Gandalf",
  "color": "grey"
}
print(character.items())

 

dict_items([('race', 'wizard'), ('name', 'Gandalf'), ('color', 'grey')])

 

 

device ={
  "vendor": "Nokia",
  "model": "7950 XRS",
  "location": "IP Core"
}
print(device.items())

 

dict_items([('vendor', 'Nokia'), ('model', '7950 XRS'), ('location', 'IP Core')])

 

 

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.

 

Back to: Python Programming Course > Python Dictionaries

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k

Python Programming Course

Collapse
Expand