0

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.

2 Answers 2

3
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 
Sign up to request clarification or add additional context in comments.

1 Comment

You'd have my upvote if you could get rid of [ & ] (generator expressions & early exit)..
2

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.