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:

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

x = CodesCracker.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:

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

print(CodesCracker.items())

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

print(CodesCracker.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 »



Like/Share Us on Facebook 😋