Dictionaries in Python in 5 mins
What is a Dictionary or Dict?
A dictionary is a data type used to store key-value pairs as we have in our English dictionary. It is useful when we want to save the data related to an entity at the same place. It is like saving an excel row in python in a column_header:column_value format
E.g
If we want to store the details(name, class, marks, address) of a student we can create different variables for this, or we can create only one variable of type dict and save the values in it.
Syntax:
s1={‘Name’:’Tarun’,‘Class’: 12,‘Address’:’house number 11 xyz street ‘,‘marks’:[90,89,67,84]}
As in the above code, we can see that a dictionary can save multiple data types. Variable “Name” is of type string, “Class” is of type int, “Address” is of type string, “Marks” is of type list.
A dict can also store a dictionary in it and is called Nested Dictionary.
Syntax:
<variable name>=dict({key:value,key:value})<variable name>={key:value,key:value}
A variable inside the dict can be accessed using its name
E.g
#to get the from the dictionary s1 created aboves1[‘Name’]#to get the marks from dict s1s1[‘Marks’]
Adding a key to the dict
# to add a new variable in the dict
s1[‘new_variable_name’]=new_variable_value
Removing a key from the dict
# to remove a variable from the dictionarydel(s1[‘variable name to be removed’])
Some Functions in Dictionary
clear():
Remove all items from the dictionary
keys():
Return a new view of the dictionary's keys
values():
Return a new view of the dictionary's values
copy():
Returns a copy of the dictionary
items():
Returns a list containing a tuple for each key-value pair
Conclusion
I hope you got an understanding of Dictionaries in python. Feel free to ask any doubt if you have.
Also, have a look at other related posts:
You re in motivation behind fact an on-target site administrator. The site stacking speed is amazing. It kind of feels that you're doing a specific trick. What's more, The substance is a masterpiece. you have done a marvelous development concerning this issue!
ReplyDeletePMP
This is a great motivational article. In fact, I am happy with your good work. They publish very supportive data, really. Continue. Continue blogging. Hope you explore your next post
ReplyDeletehrdf claimable
You have worked superbly with your site
ReplyDeletehttps://360digitmg.com/course/project-management-professional-pmp
Happy to visit your blog, I am by all accounts forward to more solid articles and I figure we as a whole wish to thank such huge numbers of good articles, blog to impart to us.
ReplyDeletehttps://360digitmg.com/india/data-science-using-python-and-r-programming-in-delhi
I will truly value the essayist's decision for picking this magnificent article fitting to my matter.Here is profound depiction about the article matter which helped me more.
ReplyDeletecyber security course in malaysia
Thanks for providing valuable and informative piece of content on Python. Read whole content, so good!
ReplyDeleteMastering Data Science with Python: A Comprehensive Guide
Great content! Thanks for sharing this useful information.
ReplyDeletePython Training Institute in Kolkataand many more cities in india.