首页 > 解决方案 > 将列表中的值与列表中的字典值进行比较

问题描述

我在列表中有一本字典:

list1 = [{'Value': 'John','Key': 'Name'},{'Value':'17','Key': 'Number'}]

我有一个清单:

list2 = ['Name','Number']

如何检查list2中的值存在于list1中。如果存在,我需要列出Value

例如:如果存在 Name ,则打印“ John

标签: pythonpython-3.x

解决方案


另请阅读评论:

for i in list2: #iterate through list2
        for j in list1: #iterate through list of dictinaries
            if i in j.values(): #if value of list2 present in the values of a dict then proceed
                print(j['Value'])

推荐阅读