Below are the Python Dictionary methods and each method is explained with examples.
method | description |
---|---|
len() | This function returns the number of items in the dictionary. |
dict() | This method is used to create a dictionary in Python. |
clear() | This method removes all items from the dictionary. |
copy() | This method returns a copy of a dictionary in Python. |
fromkeys(sequence[, value]) | This method creates a new dictionary from the given sequence of elements with a value provided by the user. |
get(key[, value]) | This method returns the value for the given key, if present in the dictionary. If not, it returns a default value. |
items() | This method returns a new object of the dictionary’s items in (key, value) format. |
keys() | This method returns a new object of the Python dictionary’s keys. |
pop(key[, default]) | This method removes an item with the provided key and returns the value. If the key is not found, it returns the default value. |
popitem() | This method removes and returns an arbitrary (key, value) pair from the dictionary. |
setdefault(key[, default]) | This method returns the value of a key (if the key is in a dictionary). If not, it inserts the key with a value to a Python dictionary. |
update([other]) | This method updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs. |
values() | Returns a view object displaying a list of a python dictionary’s values. |
I hope you can implement these dictionary methods in Python now.
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.