Click for All Topics
.items() method doesn’t take any argument and it returns the view object that contains the list of tuple and each tuple contains key-value pair.
Syntax –
dictionary.items()
Parameters –
Example –
				
					dct={'even':[2,4,6,8,10],'odd':[1,3,5,7,9]}
items=dct.items()
print(items)
# Output -
# dict_items([('even', [2, 4, 6, 8, 10]), ('odd', [1, 3, 5, 7, 9])]) 
				
			
		.items() method with dictionary and find all items of the dictionary store the result in items variable.
Example – with empty dictionary
				
					my_dict={}
items=my_dict.items()
print("Items:",items)
# Output -
# Items: dict_items([]) 
				
			
		Example –
				
					my_dict={'1':'amit',2:'abhay',3:'aditya',4:'aman'}
items=my_dict.items()
for i , j in items:
	print(i,' ',j)
# Output -
# 1   amit
# 2   abhay
# 3   aditya
# 4   aman 
				
			
		© 2021 edSlash. All Rights Reserved.