Reputation: 3299
The idea is to check whether the following dict inside list inside dict is empty or not.
For example, the following dict
expected_false={"all_dict": [{"A": [1], "B": [], "C": [], "D": []}]}
should return empty to be false as the key 'A' has a value of 1.
whereas, the following should return empty to be true
expected_empty={"all_dict": [{"A": [], "B": [], "C": [], "D": []}]}
I tried the following code, but it give different than what I have in mind.
all(not d for d in expected_true['all_dict'][0])
Upvotes: 0
Views: 689
Reputation: 11
Dictionaries are set of key-value pairs. Pseudo-code:
-Get value of 'all_dict' key inside of dictionary that named {expected_false}
-That 'all_dict' key has a value which is a list that contains only one item (which is an another dictionary)
-To address list item : ['all_dict'][0] (we reached first list item, which is a dictionary)
-Use dict.values() method to get all values of keys innermost dictionary
-And each of these values are also list by itself
expected_false['all_dict'][0].values()
Therefore the code above returns lists. And check everylist if there is any item in it. Then print result.
expected_false={"all_dict": [{"A": [1], "B": [], "C": [], "D": []}]}
for item in expected_false['all_dict'][0].values():
if len(item)!=0:
a=any(item)
print(a)
Upvotes: 0
Reputation: 1012
any(len(item) for item in expected_true['all_dict'][0].values())
Upvotes: 2
Reputation: 1669
This should work:
print(not any(len(expected_false['all_dict'][x]) > 0 for x in expected_false['all_dict']))
Upvotes: 1