Home > Article > Backend Development > How to use items() function in Python
The items() function in Python is a commonly used dictionary operation method. It is used to return a list of all key-value pairs in the dictionary. This function helps us iterate through all elements in the dictionary and operate or process each key-value pair. In this article, we will introduce the usage of items() function in detail and provide some specific code examples to help readers understand better.
Usage:
The usage of the items() function of the dictionary is very simple. It does not have any parameters. Just add .items() after the dictionary name. The returned result is a list of key-value pairs, where each key-value pair exists in the form of a tuple. The following is a simple example:
grades = {"Math": 90, "English": 85, "Science": 92} items = grades.items() print(items)
Run the above code, the output result is as follows:
dict_items([('Math', 90), ('English', 85), ('Science', 92)])
As can be seen from the output result, the items() function returns an object of type dict_items, and The key-value pairs in the dictionary grades are stored in the form of tuples. Next, we will introduce several common uses of the items() function in detail.
grades = {"Math": 90, "English": 85, "Science": 92} for key, value in grades.items(): print(key, ":", value)
Run the above code, the output is as follows:
Math : 90 English : 85 Science : 92
As can be seen from the output, through the items() function, we can easily traverse the dictionary All key-value pairs and output the keys and values together.
grades = {"Math": 90, "English": 85, "Science": 92} for key, value in grades.items(): if value >= 90: print(key, ":", value)
Run the above code, the output results are as follows:
Math : 90 Science : 92
As can be seen from the output results, we use the items() function and conditional judgment statements to filter out The subjects with scores greater than or equal to 90 and the corresponding scores are listed.
grades = {"Math": 90, "English": 85, "Science": 92} items_list = list(grades.items()) print(items_list)
Run the above code, the output result is as follows:
[('Math', 90), ('English', 85), ('Science', 92)]
As can be seen from the output result, we use the items() function to change the key value in the dictionary grades The pair is converted into a list and assigned to the variable items_list.
Summary:
Through this article, we learned about the usage of items() function in Python and several common application scenarios. This function can help us iterate through all key-value pairs in the dictionary and operate on them. Whether it is traversing a dictionary, filtering key-value pairs for specific conditions, or storing key-value pairs as a new list, the items() function can be of great help. After reading this article, I believe that readers will have a deeper understanding of the usage of the items() function and can flexibly apply it in actual programming.
The above is the detailed content of How to use items() function in Python. For more information, please follow other related articles on the PHP Chinese website!