Python Dictionary items() Method

Python Dictionary items() method “returns a view object that displays a list of dictionary’s (key, value) tuple pairs.”

Syntax

dictionary_name.items()

Parameters

The items() method does not take any argument; it just fetches the key and value using the items() method. Here, dictionary_name is the name of the Dictionary.

Return value

The items() method returns a view object of a list of the given dictionary’s (key and value) tuple pair.

Visual RepresentationVisual Representation of How to Use dictionary items() Method

Example 1: How to Use dictionary items()

CES = {"company": "Sony",
      "industry": "Automotive",
      "model": "Vision S"
 }

items = CES.items()

print('Dictionary items:', items)

Output

Dictionary items: dict_items([('company', 'Sony'), 
                              ('industry', 'Automotive'), 
                              ('model', 'Vision S')])

Example 2: How do items() work when modifying a dictionary?

Visual Representation of How do items() work when modifying a dictionary

CES = { "company": "Sony", "industry": "Automotive", 
        "model": "Vision S", "year": 2020 }

items = CES.items()

print('Original items:', items)

del[CES['year']]

print('Updated items:', items)

Output

Original items: dict_items([('company', 'Sony'), 
               ('industry', 'Automotive'), ('model', 'Vision S'), ('year', 2020)])

Updated items: dict_items([('company', 'Sony'), 
               ('industry', 'Automotive'), ('model', 'Vision S')])

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.