Python dictionary methods

In this post we will see all the methods of dictionary available in Python.These methods can be used to manipulate the dictionary objects in many ways.

The methods name and the brief description of the each of the method is listed below.

Name Function
clear() Clears items from the dictionary.
copy() Copies the dictionary.
fromkeys( ) Creates dictionary from the keys taken from another sequence
Keys() Returns all the keys of the dictionary object
values() Returns all the values of the dictionary object
items() Returns key value pair
popitem() Removes random item
pop() Removes specific item
update() Update the dictionary
get() Returns value of the specific key.
setdefault() Return values of the specific key but if the key doe not exist the key is set to new value

*Note here I have not shown the number of arguments each function can accept,each of the function is discussed in detail in another post you can visit the link to know more.

A small example using some of the methods above is shown below.

>>> d={23:46 , 67:134 , 'New':"NNeeww" }
>>> d.keys() #returns all the keys
dict_keys([23, 67, 'New'])
>>> d.popitem() #removes random key ,value pair
('New', 'NNeeww')
>>> d
{23: 46, 67: 134}