Dictionary-Methods-in-Python

Dictionary Methods in Python

Dictionary in Python is a very important data-structure which can have elements in the form of of key value pairs key:value.

Moreover, dictionary also provides different methods to manipulate. In this post, I will explain methods which will work on dictionary.
If you use
print(dir(dict))

Then you will see the output.

Dictionary-Methods-in-Python

 

The above output shows the methods of dictionary object in which the methods which have double underscores on both sides are for internal use by dict class.

Remaining methods are.

‘clear’, ‘copy’, ‘fromkeys’, ‘get’, ‘items’, ‘keys’, ‘pop’, ‘popitem’, ‘setdefault’, ‘update’, ‘values’

Which I will explain one by one.

1- clear() method-

clear() method of dictionary makes dictionary empty
For example see program and the output below.

Dictionary Methods in Python

2- copy() method-

You can use assignment operator to a copy dictionary. However, it create a new reference for the old dictionary and whenever you delete an element of old dictionary element of new dictionary will get deleted.
See the program and the output.
Dictionary Methods in Python

In the output you can see when element of oldd dictionary is deleted the element of newd dictionary is also deleted.

Furthermore, if you use copy() method of dictionary, new dictionary will completely be different. Moreover, if you delete an element of old dictionary oldd the element of new dictionary will not get deleted.

3- fromkeys() method-

fromkeys() method is used to create keys of a dictionary from a list for a single value.

See the program and output carefully,

Dictionary Methods in Python

In the above program you can see list l values are keys of dictionary d1 and value is None for every key when only one argument is passed inside fromkeys() method. However, if two values are passed one is list and another is a value then elements of list l is keys and values is “Even” for each key.

4- get() method-

get() method is used to retrieve value for a given key from a dictionary.
See the below program

Dictionary Methods in Python

In the above program, 3 is being passed inside the function get() as key and it returns 9.
Furthermore, if 6 is being passed inside the function get() as key and it returns None because key does not exist but it does not through an error.
If you want a custom message that key does not exist as second argument you can pass a message as the argument as you can see in the above program.
On the other hand, if you use name dictionary with square bracket to access an element i.e d[6], and key does not exist then program will through an error.

5- items() method

items() method returns key value as a tuple inside a list.

See the below program

 

Dictionary Methods in Python

6- keys() method-

keys() method of dictionary returns all keys.
See the below program and its output

Dictionary Methods in Python
7- pop() method

pop() method removes an element from a dictionary and can return in a variable.
See the below program and output

Dictionary Methods in Python

8- popitem() method-

popitem() method removes last inserted element before version 3.7 but in later versions it removes a random element.

https://www.w3schools.com/python/ref_dictionary_popitem.asp Python Dictionary popitem() Method

9- setdefault() method-

setdefault() method returns value of the corresponding passed key inside method and if value does not exist setdefault() method inserts it.

See the below program

Dictionary Methods in Python

10- update() method-

update() method inserts elements from d2 dictionary into d1 dictionary if you use
d1.update(d2)
For more see the below program

 

Dictionary Methods in Python

11- values() method-

values() method of dictionary returns all values.
See the below program and its output
Dictionary Methods in Python

 

 

 

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

©Postnetwork-All rights reserved.