Built with Tutify.io

    AIStudyLabs

    Python Dictionary items()

    Python Dictionary items()

    The Python dictionary items() method is used to return a view object of dictionary elements as tuples in a list. The items in the dictionary are unordered and mutable hence, they can be changed, removed, and added after the creation of the dictionary. However, the items cannot duplicate within the same dictionary.

    The items in the dictionary can be of any data type, including numbers and characters such as integers, strings, floats, and complex numbers. Boolean type, etc. items() method is primarily used when you want to iterate over a dictionary

    All dictionary methods are available on the dictionary methods page..

    Quick examples of Python dictionary items() method

    Following are quick examples of how to use the Dictionary items() method.

    
    courses = { 1:'java', 2:'python', 3:'pandas',4:'sparks'}
    # Example 1: Using the items() to get dictionary elements
    print(courses.items())
    
    # Example 2:Updated view object after modification of dictionary
    dict_view=courses.items()
    print("original courses:",dict_view)
    courses[5]='NumPy'
    print("new courses:",dict_view)
    

    1. Syntax of items()

    Following is the syntax of the Python Dictionary items().

    
    # Syntax of the items()
    dict.items()
    

    1.1 items() method parameters

    The Python dictionary items() method doesn't allow any parameters.

    1.2 Return value of items()

    It returns a view object that allows a dynamic view of dictionary elements as tuples in a list. When you use the items () method in the dictionary, the key-value pair stored in it will display in the form of tuples in the returned list. Moreover, If the dictionary allows any modifications using the items() method, which will reflected in the view object also.

    2. Usage of Python Dictionary items() Method

    2.2 Return a View Object of Dictionary Elements

    When you use, the items() method returns a view object of key-value pairs for the dictionary, as tuples in a list.

    
    # Using the items() to get dictionary elements
    courses = { 1:'java', 2:'python', 3:'pandas',4:'sparks'}
    print(courses.items())
    
    # Output:
    dict_items([(1, 'java'), (2, 'python'), (3, 'pandas'), (4, 'sparks')])
    

    2.3 Return a View object After Modification of Dictionary

    If the dictionary allows any modifications using the items() method, which will reflected in the view object also. Let's take an example for a better understanding.

    
    # Updated view object after modification of dictionary
    courses = { 1:'java', 2:'python', 3:'pandas',4:'sparks'}
    dict_view=courses.items()
    print("original courses:",dict_view)
    
    # Output: 
    original courses: dict_items([(1, 'java'), (2, 'python'), (3, 'pandas'), (4, 'sparks')])
    
    courses[5]='NumPy'
    print("new courses:",dict_view)
    # Output:
    new courses: dict_items([(1, 'java'), (2, 'python'), (3, 'pandas'), (4, 'sparks'), (5, 'NumPy')])
    

    Note that view object has been updated after modifying the courses dictionary.

    3. Conclusion

    In this article, I have explained how to get a dynamic view of dictionary elements by using the Python dictionary items() method. As well as how the view object has been updated after modifying the dictionary.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References