Dictionary items() method Python

The Python dictionary items() method return all the key value pair of the dictionary object.This method does not accept any argument.

Link :Python dictionary data type

>>> d1={'Tempt':'Hex' , 345:"Text" , 'Doom':1 , 'All':'New'}
>>> d1.items()
dict_items([('Tempt', 'Hex'), (345, 'Text'), ('Doom', 1), ('All', 'New')])
>>> d2=d1.items() #work fine!!
>>> d2
dict_items([('Tempt', 'Hex'), (345, 'Text'), ('Doom', 1), ('All', 'New')])

Chek if key value pair exist in dictionary

To check if a key,value pair exit in the dictionary we can use the keyword ‘in’.The format of using ‘in’ keyword to check if a key value exist in the ‘d’ object is:
 
(key,value) in d.items()
 
If the key value exist True is return else False is returned.

>>> d={2:3 , '56':56 , '12/34/3209':'Explosion' , 'New':'Era'}
>>> (2,3) in d.items()
True
>>> ('new','Era') in d.items()
False
>>> ('56' ,56) in d.items()
True


Leave a Reply

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