1

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.

3
  • Please fix the post to show the code that uses any; you copy-pasted the error message a second time instead. Commented Feb 3, 2021 at 15:23
  • But more importantly: where you wrote {key: val for key, val in sub.items() if val}, what is the actual rule that you have in mind with if 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? Commented Feb 3, 2021 at 15:25
  • You're calling array.any() when you should be doing any(array), which is why you're getting the second error. any is 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) Commented Feb 3, 2021 at 16:20

3 Answers 3

1

A common way to check empty for empty lists is to check len(list). So assuming your dict() looks like so

myDict = {
  1: [1,2,3],
  2: [],
  3: np.array([[1,2],[3,4],[5,6]])
}

Your list comprehension might look like

res = {k:v for k,v in myDict.items() if len(v)}

Note the len(v) in the dict comprehension

Sign up to request clarification or add additional context in comments.

Comments

1

I think you've made this one more step complicated than necessary (as well as not including a complete example!)

The following example creates a new dict res with all values of test_dict that have non-empty values. I used len() here because that works on both lists and nd-arrays. For just lists, I'd omit the call to len() and just use val.

test_dict = {1: [], 2: [1,2,3], 3: [4,5,6]}
res = {key: val for key, val in test_list.items() if len(val)}

If you are wanting to use any(), you'd be finding dict values that are lists that contain at least one truthy item:

test_dict = {1: [], 2: [1,2,3], 3: [4,5,6]}
res = {key: val for key, val in test_list.items() if any(val)}

Comments

0

@Jacob's answer works fine, though it is really inefficient.

Instead, you can take advantage of the built-in filter() method to filter out the empty dictionaries, and use the dict() method instead of using a dict comprehension:

res = filter(None, (dict(i for i in sub.items() if len(i[1])) for sub in test_list))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.