How to Use Dictionary items() Method in PythonPython Basics


The items() method returns a view object which displays a list of  key-value pairs in a given dictionary.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

dict1 = {'Name': 'Gerryzhang', 'Age': 24,'sex':'man','ID':'2019'}

print(dict1.items())

Output:

dict_items([('Name', 'Gerryzhang'), ('Age', 24), ('sex', 'man'), ('ID', '2019')])

Syntax

dictionary.items()

Parameters

The method doesn’t take any parameters.

Return Value

It returns a list of key-value pairs in a given dictionary.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us