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

What are several traversal methods of dict in Python dictionary?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you about what are several ways of traversing dict in the Python dictionary. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

1. Use for key in dict to traverse dictionaries

You can use for key in dict to traverse all the keys in the dictionary

X = {'asides:' Aids, 'baked:' B'} for key in x: print (key) # output result ab2. Use for key in dict.keys () to traverse the keys of the dictionary

The dictionary provides the keys () method to return all the keys in the dictionary

# keysbook = {'title':' Python', 'author':' -', 'press':' life is too short, I use python'} for key in book.keys (): print (key) # to output the result titleauthorpress3. Use for values in dict.values () to traverse the values of the dictionary

The dictionary provides the values () method to return all the values in the dictionary

A: there are no answers to problems encountered in study. The editor has created a Python learning exchange group: 725638078 look for like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! # valuesbook = {'title':' Python', 'author':' -', 'press':' Life is short, I use python'} for value in book.values (): print (value) # output result Python- Life is short, I use python4. Use for item in dict.items () to traverse the dictionary's key-value pair

The dictionary provides the items () method to return all the key-value pairs in the dictionary item

Key value is a tuple for item (item 0 is the key, item 1 is the value)

X = {'avers:' Aids, 'bundles:' B'} for item in x.items (): key = item [0] value = item [1] print ('% s% slug% s'% s'% (item, key, value)) # output result ('asides,' A') aRover A ('baked,' B') b:B5. Use for key,value in dict.items () to traverse the dictionary's key-value pair

You can omit parentheses when the tuple is to the right of the = assignment operator

A: there are no answers to problems encountered in study. The editor has created a Python learning exchange group: 725638078 look for like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! Item = (1,2) a, b = itemprint (a, b) # output result 1 2

Example:

X = {'averse:' aversion, 'breadth:' B'} for key, value in x.items (): print ('% slug% s'% (key, value)) # output result a:Ab:B above is what the dict traversal methods are in the Python dictionary shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are 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

Development

Wechat

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

12
Report