Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the traversal of Dictionary by Python

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces Python how to achieve dictionary traversal, the article is very detailed, has a certain reference value, interested friends must read it!

The traversal dictionary is mainly about traversing the entries, keys, and values of the dictionary. The key of traversing the dictionary is to list all the items contained in the dictionary, and the key and value of traversing the dictionary is to list all the keys and values of the dictionary. The items method provided by the dictionary gets the collection of all the items in the dictionary, and the items method can be used in the for loop statement to traverse all the items contained in the dictionary.

Traversing with for

Similar to other data structures, we can use for to traverse elements.

Dict_= {'key1':'value1','key2':'value2'} for i in dict_: print (I, end='\ t') for i in dict_: print (dict_ [I], end='\ t')

The first traversal takes the key, the second traversal takes the value

The output is

Key2 key1value2 value1 traverses the keys and values of the dictionary

Using keys, values can return iterations of all values of a dictionary

Dict_= {'key1':'value1','key2':'value2'} for key in dict_: print (key, end='\ t') for value in dict_.values (): print (value, end='\ t')

Running result:

Key2 key1value2 value1 traverses entries in the dictionary

Using items, you can convert the dictionary type to the corresponding tuple list, and then traverse the dictionary.

Dict_= {'key1':'value1','key2':'value2'} item=dict_.items () print (item) for key,value in item: print (key,value)

The results are as follows:

Dict_items ([('key2','value2'), (' key1','value1')]) key2 value2key1 value1 above is all the content of the article "how to traverse the dictionary by Python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report