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 does python read the value of dictionaries in the list

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how python reads the value of dictionaries in the list. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Read the value value of the dictionary in the list list = [{"name": "recommended recipe", "1": "symptom", "name1": "hot and cold all over"}, {"name": "mung bean rice"}, {"name": "sesame"}] res = [for item in list for key in item] print (res) for item in list: for key in item: print (itemkey])

List = [{"name": "recommended recipe", "1": "symptom", "name1": "hot and cold all over"}, {"name": "mung bean rice"}, {"name": "sesame"}] res = [item[ key] for item in list for key in item] print (res) for item in list: for kcatalog v in item.items (): print (KParry v)

Get the key and value of the dictionary

In an iterative way, by default, the dictionary iterates over the key,key equivalent to the [0] position in item, and value is the [1] position.

A.items ()

Key=a.items [0]

Value=a.items [1]

For item in dict.items (): key = item [0] value = item [1]

Print each key

For item in a.items (): print (item [0]) > abc

Print each value

For item in a.items (): print (item [1]) > > 123

Instead of using the function dict.item (), you can iterate directly by using the feature that the default iteration is key.

> dict = {'averse: 1,' baked: 2, 'cached: 3} > for key in dict:... Print (key)... acb

In the second way, if you want to iterate over value, you can use a.values (), which is to find the value, and the finding key is a.keys ()

A = {'axianglu 1, print print (key) > for value in a.values (): print (key) > > abc

Third, if you want to iterate through both key and value, you can use for k, v in d.items ().

A = {'axiaqiaqizhuo 1 print a1b2c3 > a1b2c3

Output all key and value, but not in list format

A = {'axianglanglanglingzhonglanglinglanglinglinglinglanglinglanglinglanglinglinglinglingzhongzhuangzhuangzhuangzhuo > dict_values ([1,2,3]) print (a.keys ()) > dict_keys ([' aura, 'baked,' c'])

If you want to become a list list, you can convert the type.

List (a.keys ()) > ['await,' baked,'c'] this is the end of the article on "how to read the value of dictionaries in the list by python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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