Python Add To Dictionary

Python-Add-To-Dictionary-ipcisco-1

In this lesson, we will learn How to add a member to Python Dictionary. We will do different coding examples like Python dictionary add and python dictionary append. We can use these methods to insert a new item. So, let’s see the example of the usage of this methods.

 

Firstly, let’s change the value of a dictionary key:pair value.

 

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

 

The output of this python code will be:

 

{'vendor': 'Cisco', 'model': '4000 series', 'RU': 44}

 


You can also check all Python Dictionary Iteration Lessons


 

Python-Add-To-Dictionary-ipcisco-1

 


 

Table of Contents

Append Method

 

We can also use the below code to insert values to the existing dictionary. This can be used as python append method. Normally there is no append method of python dictionaries.

 

device = {"vendor":["Cisco"],"model":["9000 series"],"RU":[44]};
device["model"].append("4000 series")
device["RU"].append(30)          
print(device)

 

{'vendor': ['Cisco'], 'model': ['9000 series', '4000 series'], 'RU': [44, 30]}

 

Python-Add-To-Dictionary-ipcisco-2

 

In the below example, again we will change the key:pair value.

 

character = {"race":["dwarf"],"name":["Gimli", "Balin"],"role":["warrior"]};
character["race"].append("elf")
character["name"].append("legolas")
print(character)

 

{'race': ['dwarf', 'elf'], 'name': ['Gimli', 'Balin', 'legolas'], 'role': ['warrior']}

 

 

Update Method

 

We can use update method to add or change the values of a key:value pair. In the below example, we will update the content of the dictionary with update method.

 

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

 

The output of this python code will be like below. It will contain the updated member of the dictionary. In other words, it will contain new value of RU as 30.

 

{'vendor': 'Cisco', 'model': '9000 series', 'RU': 30}

how-to-add-To-Dictionary-in-python

Update method can get only one attribute. If you use one more attribute, it will give an error as output. To update one more key:pair, we should use differenet update lines.

 

Let’s do another example with python dictionary update method and change all the key:value pairs. In the below example, we will change or update all the content of the dictionary with new values.

 

device ={
  "vendor": "Cisco",
  "model": "9000 series",
  "RU": 44
}
device.update({"vendor": "Nokia"})
device.update({"model": "7950 XRS"})
device.update({"RU": "39"})
print(device)

 

The output of this python code will be like below.

 

{'vendor': 'Nokia', 'model': '7950 XRS', 'RU': '39'}

 

Here, we have learned the methods that we can insert items to the python dictionaries in Python Programming. You can use these python dictionary add methods on your various code examples. You can do different coding exaamples o this lesson. More practice will give you more experience on such methods. Dictionaries are one of the important lessons of Python Programming Course.

 

Back to: Python Programming Course > Python Dictionaries

Leave a Reply

Your email address will not be published.

IPCisco is the Winner of 2019 “Best Certification Study Journey” Category! We are also Finalist of 2020 & 2021 in Cisco IT Blog Awards!

Cisco-ITBlogAwards-2019-Winner-IPCisco-k
ITBlogAwards_2020_Badge-Finalist-BestCertStudy-ipcisco
ITBlogAwars-2021-ipcisco

Python Programming Course

Collapse
Expand
IPCisco on Social Media!

Latest Blog Posts

top-ten-network-certifications-ipcisco
metaverse-jobs-ipcisco.com
Wifi-6-vs-wifi-5-ipcisco.com
16-Reasons-To-Learn-Linux
gns3-vm-ware-installation-guide