Python Programming – Dictionary Methods

In this Page, We are Providing Python Programming – Dictionary Methods. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Python Programming – Dictionary Methods

Dictionary methods

The following are some dictionary methods supported by Python.

diet . clear ( )
Removes all items from the dictionary.

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }
>>> len ( a )
2 
>>> a . clear ( )
>>> len ( a )
0

diet. copy ( )
Returns a copy of the dictionary.

>>> dict1 = { ' Name ' : ' Zara ' , ' Age ' : 7 ]
>>> dict2=dict1 . copy ( )
>>> dict2
{ ' Age ' : 7 , ' Name ' : ' Zara ' }

diet. fromkeys ( seq [ , value ] )
Create a new dictionary with keys from seq and values set to value (default as None).

>>> seq= ( ' name ' , ' age ' , ' gender ' )
>>> a=dict . fromkeys ( seq )
>>> a 
{ ' gender ' : None , ' age ' : None , ' name ' : None }
>>> a=a.fromkeys ( seq , 10 )
>>> a
{ ' gender ' : 10 , ' age ' : 10 , ' name ' : 10 }

diet . get ( key [ , default ] )
Return the value for the key, if the key is in the dictionary, else default. If the default is not given, the default is None.

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }
>>> a.get ( ' Age ' )
7
>>> a. get ( ' Gender ' , ' Never ' )
' Never '
>>> print a.get ( ' Gender ' )
None

diet.has_key ( key )
Test for the presence of a key in the dictionary. Returns True, if the key is present in the dictionary, otherwise False.

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }
>>> a.has_key ( ' Age ' )
True
>>> a.has_key ( ' Gender ' )
False

diet.items ( )
Returns a list of dictionary’s key-value tuple pairs.

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 } 
>>> a . items ( )
[ ( ' age ' , 7 ) , ( ' Name ' , ' Zara ' ) ]

diet.iteritems ( )
Returns an iterator over the dictionary’s key-value pairs.

>>> a= { ' Gender ' : ' Female ' , ' Age 1 : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' } 
>>> for b in a . iteritems ( ) :
. . . print ' { 0 } - - - -{ 1 } ' . format ( b [ 0 ] , b [ 1 ] )
. . .
Gender - - - -Female 
Age - - - - 7 
Hair color- - - - None1
Name- - - - Zara

diet.iterkeys ( )
Returns an iterator over the dictionary’s keys.

>>> a= { 1 Gender 1 : ’ Female ' , ' Age ' : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' }
>>> for b in a.iterkeys ( ) :
. . . print b
. . . 
Gender
Age 
Hair color 
Name

diet.itervalues ( )
Returns an iterator over the dictionary’s values.

>>> a= { 1 Gender ' : ' Female ' , ' Age ' : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' }
>>> for b in a.itervalues ( ) :
. . . print b
. . .
Female
7
None
Zara

diet.keys ( )
Returns list of dictionary keys.

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }
>>> a . keys ( )
[ ' Age ' , ' Name ' ]

diet.pop ( key [ , default ] )
If key is.in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.

>>> a= { ' Gender ' : ' Female ' , ' Age ' : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' }
>>> a . pop ( ' Age ' , 15 )
7
>>> a . pop ( ' Age ' , 15 )
15 
>>> a
{ ' Gender ' : ' Female ' , ' Hair color ' : None , ' Name ' : ' Zara ' }

diet. popitem ( )
Remove and return an arbitrary key-value pair from the dictionary. If the dictionary is empty, calling popitem () raises an KeyError exception.

>>> a= { ' Gender ' : ' Female ' , ' Age ' : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' } 
>>> a . popitem ( )
( ' Gender ' , ' Female ' )
>>> a . popitem ( )
( ' Age ' , 7 )
>>> a
{ ' Hair color ' : None , ' Name ' : ' Zara ' }

diet . setdefault ( key [ , default ] )
If key is in the dictionary, return its value. If not, insert key with a value of default and return default. The default defaults to None.

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }
>>> a . setdefault ( ' Age ' , 15 )
7
>>> a . setdefault ( ' Gender ' , ' Female ' )
' Female '
>>> a . setdefault ( ' Hair color ' )
>>> a
{ ' Gender ' : ' Female ' , ' Age ' : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' }

diet.update ( [ other ] )
Update the dictionary with the key-value pairs from other, overwriting existing keys, and returns None. The method accepts either another dictionary object or an iterable of key-value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key-value pairs.

>>> dict1= { ' Name ' : ' Zara ' , ' Age ' : 7 }
>>> dict2= { ' Gender ' : ' female ' }
>>> dict1 . update ( dict2 )
>>> dict1 . update ( hair_color= ' black ' , eye_color= ' blue ' )
>>> dict1
{ ' eye_color ' : ' blue ' , ' Gender ' : ' female ' , ' Age ' : 7 , ' Name ' : ' Zara ' , ' hair_color ' : ' black ' }

diet . values ( )
Return list of dictionary values.

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }
>>> a . values ( )
[ 7, ' Zara ' ]