Home  >  Article  >  Backend Development  >  How to use items() function in Python

How to use items() function in Python

王林
王林Original
2024-02-19 10:35:06760browse

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.

  1. Traverse all key-value pairs of the dictionary
    We can use a for loop combined with the items() function to traverse all key-value pairs of the dictionary and operate on them. The following is an example:
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.

  1. Use the items() function to filter
    The items() function can also be used with conditional judgment statements to help us filter out key-value pairs that meet specific conditions. The following is an example:
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.

  1. Storing the key-value pairs of the dictionary as a list
    Sometimes, we need to store the key-value pairs in the dictionary as a new list and perform further operations on it. Using the items() function, we can easily implement this function. The following is an example:
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn