items(), takes no argument ( inputs ) .
Returns : the list view object with tuple of key value pairs using all elements of dictionary.
The original dictionary remain same without any change.
my_dict={'a':'One','b':'Two','c':'Three'}
x=my_dict.items()
print(x)
print(type(x))
Output is here
dict_items([('a', 'One'), ('b', 'Two'), ('c', 'Three')])
<class 'dict_items'>
Matching value and showing key
my_dict = {"a": "One", "b": "Two", "c": "Three"}
for i, j in my_dict.items():
if j == "Two":
print(i, j) # b Two
View object
The view object created by items() method will reflect the changes in dictionary.
my_dict={'a':'One','b':'Two','c':'Three'}
x=my_dict.items()
print(x)
del(my_dict['b']) # deleted one item with key b
print(x)
Output
dict_items([('a', 'One'), ('b', 'Two'), ('c', 'Three')])
dict_items([('a', 'One'), ('c', 'Three')])
« All dictionary methods
← Subscribe to our YouTube Channel here