Python items() Function

The items() function in Python returns a view object that is basically the list of items available in the specified dictionary, where the returned items are in tuples of key and value. For example:

fresherearth = {
    "Name": "Sophia",
    "City": "Liverpool",
    "Course": "EECS",
    "Age": "20" }

x = fresherearth.items()
print(x)

The output will be:

dict_items([('Name', 'Sophia'), ('City', 'Liverpool'), ('Course', 'EECS'), ('Age', '20')])

View objects provides dynamic view on the entries of dictionary. Therefore, when the dictionary changes, the view definitely reflects the change.

Python items() Function Syntax

The syntax of items() function in Python, is:

dictionaryName.items()

Python items() Function Example

Here is an example of items() function in Python:

fresherearth = {"Name": "Sophia", "Age": "20"}

print(fresherearth.items())

print("\nEnter the current age to update: ", end="")
ag = input()
fresherearth["Age"] = ag

print(fresherearth.items())

The snapshot given below shows the sample run of above program, with user input 22:

python items function

Python Online Test


« Previous Function Next Function »