items(): a view object displaying a list of a dictionary's key-value pairs as tuples

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')])

Loop Through a Dictionary Using items() Method

The items() method allows us to iterate over both keys and values in a dictionary.

Example Code

my_dict = {
    "name": "Alice",
    "age": 25,
    "city": "New York"
}

# Loop through dictionary and display key-value pairs
for key, value in my_dict.items():
    print(key, "=>", value)

Expected Output

name => Alice
age => 25
city => New York

Explanation

  • my_dict.items(): Returns key-value pairs from the dictionary.
  • for key, value in my_dict.items(): Iterates over each pair.
  • print(key, "=>", value): Displays the key and its corresponding value.

Use Case

This method is useful for processing data stored in dictionaries, such as user details, configuration settings, or database records.


All dictionary methods
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer