Professional Documents
Culture Documents
Only thing that is not allowed is, you cannot defined a key in square brackets for example
my_dict = {["Name"]:"ABC","Address":"Mumbai","Age":30};
my_dict = {"Name":[],"Address":[],"Age":[]};
The keys in the dictionary are Name, Address and Age. Usingappend() methodwe canupdate
the values for the keys in the dictionary.
my_dict = {"Name":[],"Address":[],"Age":[]};
my_dict["Name"].append("Guru")
my_dict["Address"].append("Mumbai")
my_dict["Age"].append(30)
print(my_dict)
When we print the dictionary after updating the values, the output is as follows:
Output:
Here is an example that shows to accesselements from the dictionary by using the key in the
square bracket.
Output:
username : XYZ
email : xyz@gmail.com (mailto:xyz@gmail.com)
location : Mumbai
If you try to use a key that is not existing in the dictionary , it will throw an error as shown
below:
Output:
The syntax is :
del dict['yourkey'] # This will remove the element with your key.
To delete the entire dictionary, you again can make use of the del keyword as shown below:
del my_dict # this will delete the dictionary with name my_dict
To just empty the dictionary or clear the contents inside the dictionary you can makeuse of
clear() method on your dictionaryas shown below:
your_dict.clear()
Here is a working example that shows the deletion of element, to clear the dict contents and
to delete entire dictionary.
Output:
Syntax:
dict.pop(key, defaultvalue)
The pop() method returns the element removed for the given key, and if the given key is not
present, it will return the defaultvalue. If the defaultvalue is not given and the key is not
present in the dictionary, it will throw an error.
Output:
my_dict['name']='Nick'
print(my_dict)
Output:
my_dict["username"] = "ABC"
print(my_dict)
Output:
Dictionary 1:
Dictionary 2:
Now I want my_dict1 dictionary to be inserted into my_dict dictionary. To do that lets create
a key called "name" in my_dict and assign my_dict1 dictionary to it.
Here is a working example that shows inserting my_dict1 dictionary into my_dict.
my_dict["name"] = my_dict1
print(my_dict)
Output:
Now if you see the key "name", it has the dictionary my_dict1.
Summary:
Dictionary is one of the important data types available in Python. The data in a
dictionary is stored as a key/value pair. The key/value is separated by a colon(:), and the
key/value pair is separated by comma(,). The keys in a dictionary are unique and can be a
string, integer, tuple, etc. The values can be a list or list within a list, numbers, string, etc.
Method Description
Next (/python-operators-complete-tutorial.html)
Python Tutorials
Python Dictionary (/python-dictionary-beginners-tutorial.html)
(https://www.facebook.com/guru99com/)
(https://twitter.com/guru99com)
(https://www.linkedin.com/company/guru99/)
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)
(https://forms.aweber.com/form/46/724807646.htm)
About
About Us (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact Us (/contact-us.html)
Career Suggestion
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Interesting
eBook (/ebook-pdf.html)
Blog (/blog/)
Quiz (/tests.html)
SAP eBook (/sap-ebook-pdf.html)
Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)