Home > Back-end >  How to remove zero values from arrays in dictionary
How to remove zero values from arrays in dictionary

Time:11-11

Suppose I have a dictionary containing 2d arrays:

dict = {
    "a": np.array([[0, 2, 3], [4, 0, 6]]),
    "b": np.array([[1, 0, 3], [4, 5, 0]]),
    "c": np.array([[1, 2, 0], [0, 5, 6]])}

And I want to remove zero values from each array in that dictionary. The output I want to get should look like this:

dict = {
    "a": np.array([[2, 3], [4, 6]]),
    "b": np.array([[1, 3], [4, 5]]),
    "c": np.array([[1, 2], [5, 6]])}

How can I do that?

CodePudding user response:

{k: v for k, v in d.items() if v}

removing False, [], None also

  • Related