简体   繁体   中英

How to check a value is existing in a list of dictionarys in python

I have a list of dicts like this:

   l = [{"1": "one", "2":"two"}, {"3": "three", "4":"four"}, {"5": "five", "6":"six"}]

I want to check the below value is exist in that list of dicts or not:

     a = "two"

I know we can do this by loop and get() method, I am expecting any alternate method.

In [5]: l = [{"1": "one", "2":"two"}, {"3": "three", "4":"four"}, {"5": "five", "6":"six"}]
In [7]: any(["two" in i.values() for i in l])
Out[7]: True

How about this one-liner?

>>> l = [{"1": "one", "2":"two"}, {"3": "three", "4":"four"}, {"5": "five", "6":"six"}]
>>> any(y == "two" for x in l for y in x.values())
True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM