Home  >  Article  >  Backend Development  >  Using the items() method of a dictionary in Python

Using the items() method of a dictionary in Python

WBOY
WBOYOriginal
2024-02-19 22:46:22527browse

Using the items() method of a dictionary in Python

The dictionary in Python is an unordered key-value pair data structure used to store a set of related data. Dictionaries in Python provide a wealth of methods and operators to operate on dictionaries. Among them, the items() method is a very commonly used method, which is used to return a list of all key-value pairs in the dictionary. This article will introduce the usage of items() method in detail and give specific code examples.

The items() method can be used to return all key-value pairs in the dictionary in list form. Each key-value pair is represented as a tuple, with the key first and the value last. The basic syntax of the items() method is as follows:

dictionary.items()

National dictionary example:

country_dictionary = {
    "中国": "北京",
    "美国": "华盛顿",
    "日本": "东京",
    "韩国": "首尔",
    "德国": "柏林"
}

To use the items() method, just add .items() after the dictionary name. The following is an example of using the items() method:

country_dictionary_items = country_dictionary.items()
print(country_dictionary_items)

Running the above code will output the following results:

dict_items([('中国', '北京'), ('美国', '华盛顿'), ('日本', '东京'), ('韩国', '首尔'), ('德国', '柏林')])

As you can see, the items() method returns a dict_items object. The object can be converted to a list, or each key-value pair can be obtained directly by looping through it.

If you want to convert the dict_items object into a list, you can use the list() method. For example:

country_dictionary_items_list = list(country_dictionary_items)
print(country_dictionary_items_list)

Run the above code, the following results will be output:

[('中国', '北京'), ('美国', '华盛顿'), ('日本', '东京'), ('韩国', '首尔'), ('德国', '柏林')]

As you can see, after converting the dict_items object into a list, a list containing all key-value pairs is obtained. Each key-value pair is still a tuple.

Another common usage is to traverse the dict_items object through a for loop and obtain each key-value pair one by one.

for key, value in country_dictionary.items():
    print(key, value)

Run the above code, the following results will be output:

中国 北京
美国 华盛顿
日本 东京
韩国 首尔
德国 柏林

Through the for loop, you can obtain each key-value pair one by one to perform related operations.

To sum up, the items() method is one of the very practical methods in the Python dictionary. It can return all key-value pairs in the dictionary in list form, which facilitates us to process and operate the dictionary data. At the same time, each key-value pair can be easily obtained one by one through a for loop. Hope this article is helpful for using the items() method of dictionary.

The above is the detailed content of Using the items() method of a dictionary 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