I want to eliminate all empty value from a dict whose values are a mix of lists and nd array. So I tried with:
res = [ele for ele in ({key: val for key, val in sub.items() if val} for sub in test_list) if ele]
but I get the error
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(). And if I try:
AttributeError: 'list' object has no attribute 'any'
I get the error
AttributeError: 'list' object has no attribute 'any'
So I am wondering if there is a more general way to delete empty values in python dict.
any; you copy-pasted the error message a second time instead.{key: val for key, val in sub.items() if val}, what is the actual rule that you have in mind withif val? With plain lists, an empty list will fail this and every other possible list will pass; is that what you intend? With the Numpy arrays, which should pass and which should fail?array.any()when you should be doingany(array), which is why you're getting the second error.anyis a built in function and not an attribute of a generic python array. (Fixing this is probably not going to fix your function but it is the cause of the error)