[英]How to compare a value with a list of dicts without using for loop in python?
Input:输入:
value = "apple"
dict = [{'name':'apple','color':"red"},{'name':'orange','color':"orange"}]
Witout using the for loop like below, is it possible to compare and get the values?如果不使用下面的 for 循环,是否可以比较并获取值?
Code I have done:我做过的代码:
for i in dict:
if i["name"] == value:
print i
You do have multiple dicts in a list, not just one.您确实在列表中有多个字典,而不仅仅是一个。 Therefore you must go through all items in your list.
因此,您必须通过列表中的所有项目 go。
for d in dict: # dict is actually a list, but I am using your var name
if value in d.values():
print(value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.