Introduction
Welcome to my 34th blog post. In the last blog, I covered what a dictionary is. In today's blog, I am going to share my learnings from Day #34 of my python coding journey. On day 34 I learned about the most commonly used Dictionary methods in python. Let's dive into more details.
So let's get started......
Dictionary Methods
The dictionary uses several built-in methods for manipulation. The most commonly used dictionary methods are listed below
update()
clear()
pop()
popitem()
del
update()
update() method updates the value of the key provided to it.
If the item already exists in the dictionary then the key value is updated, else it creates a new key-value pair.
Example -
info = {'name':'Karan', 'age':19, 'eligible':True}
print(info)
info.update({'age':20})
info.update({'DOB':2001})
print(info)
Output -
{'name': 'Karan', 'age': 19, 'eligible': True}
{'name': 'Karan', 'age': 20, 'eligible': True, 'DOB': 2001}
Create an empty dictionary
Example -
empt = {}
print(empt)
{}
Remove items from the dictionary:
There are a few methods that we can use to remove items from the dictionary.
clear()
This method removes all the items from the list.
info = {'name':'Karan', 'age':19, 'eligible':True}
info.clear()
print(info)
{}
pop()
This method removes the key-value pair whose key is passed as a parameter.
info = {'name':'Karan', 'age':19, 'eligible':True}
info.pop('eligible')
print(info)
{'name': 'Karan', 'age': 19}
popitem()
This method removes the last key-value pair from the dictionary.
info = {'name':'Karan', 'age':19, 'eligible':True, 'DOB':2003}
info.popitem()
print(info)
{'name': 'Karan', 'age': 19, 'eligible': True}
del
'del' keyword is used to remove a dictionary item.
info = {'name':'Karan', 'age':19, 'eligible':True, 'DOB':2003}
del info['age']
print(info)
{'name': 'Karan', 'eligible': True, 'DOB': 2003}
Note: If the key is not provided, then the del keyword will delete the dictionary entirely.
Example -
info = {'name':'Karan', 'age':19, 'eligible':True, 'DOB':2003}
del info
print(info)
NameError: name 'info' is not defined
Resources Used
You can watch the video of Day#34 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. Through this Blog post, we learned about various Dictionary methods in Python and wrote a whole bunch of code to practice the dictionary methods.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of python coding. This is it for Day #34
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.