4

I'm doing this in Python 3.8 and I havent been able to fine a clean solution.

I am trying to remove any values from a nested dictionary if the value is equal to 0.

So the nested dictionary kinda looks like this:

{1: {"alert1" : 0, "alert2": 1, "alert3" : 3, "alert4": 1},
2: {"alert1" : 45, "alert2": 2, "alert3" : 0, "alert4": 54},
3: {"alert1" : 2, "alert2": 1, "alert3" : 33, "alert4": 11},
4: {"alert1" : 1, "alert2": 0, "alert3" : 2, "alert4": 0}}

And so if I want to print it or view it but only if an alert is not 0 so print(nested_dic) would look like this.

1 - alert2: 1    alert3: 3    alert4: 1
2 - alert1: 45   alert2: 2    alert4: 54
3 - alert1: 2    alert2: 1    alert3: 33    alert3: 11
4 - alert1: 1    alert3: 2    

EDIT: Either delete the non zeroes or save a new dictionary without zeroes to be printed out.

3
  • Do you want to modify the dictionary to delete 0 entries, or do you just want it to print non-zero entries? Commented Jun 24, 2020 at 20:20
  • Ahh I probably should have made that clear. Modifying the dictionary would not impact it at all, but I was just looking for a way to print out the nonZeroes. Though I think now deleting the zeroes might make it easier in the long run Commented Jun 24, 2020 at 20:22
  • Can you show your attempt and explain the failure? Commented Jun 24, 2020 at 22:10

2 Answers 2

2

If you only have one level of nesting I think using a comprehension inside a comprehension is still readable, so I would suggest this (d is your dictionary here):

>>> {k: {k_sub: v_sub for k_sub, v_sub in v.items() if v_sub != 0} for k, v in d.items()}

{1: {'alert2': 1, 'alert3': 3, 'alert4': 1},
 2: {'alert1': 45, 'alert2': 2, 'alert4': 54},
 3: {'alert1': 2, 'alert2': 1, 'alert3': 33, 'alert4': 11},
 4: {'alert1': 1, 'alert3': 2}}

But maybe you prefer code that is more detailed and add more names so it's easier to read:

def non_zero(alerts):
    return {code: value for code, value in alerts.items() if value != 0}

all_alerts = {1: {"alert1" : 0, "alert2": 1, "alert3" : 3, "alert4": 1},
2: {"alert1" : 45, "alert2": 2, "alert3" : 0, "alert4": 54},
3: {"alert1" : 2, "alert2": 1, "alert3" : 33, "alert4": 11},
4: {"alert1" : 1, "alert2": 0, "alert3" : 2, "alert4": 0}}

all_non_zero_alerts = {identifier: non_zero(alerts) for identifier, alerts in all_alerts.items()}

This does the exact same thing as before but it may be a bit more clear what the code is achieving.

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

Comments

1

My solution is to use a list comprehension. This is best for iterating and removing any unwanted items. Here's an example of how this would work:

dic = {1: {"alert1" : 0, "alert2": 1, "alert3" : 3, "alert4": 1},
2: {"alert1" : 45, "alert2": 2, "alert3" : 0, "alert4": 54},
3: {"alert1" : 2, "alert2": 1, "alert3" : 33, "alert4": 11},
4: {"alert1" : 1, "alert2": 0, "alert3" : 2, "alert4": 0}}

for key in dic:
    value = dic[key]
    keys = {k:v for (k, v) in value.items() if v != 0}
    print(keys)

Here, the only filtering you're doing happens on that for iteration loop, where you loop through all keys if the value isn't 0.

This solution is preferable since it uses the Pythonic dictionary comprehension, which allows you to neatly iterate over each items in the individual dict in a simple line. Relative to loops, this is a more beneficial approach since Python can easily optimize list comprehensions to improve efficiency.

Variations

If you want to modify the existing dictionary, you can simply modify the key while you're iterating over it, as follows:

for key in dic:
    value = dic[key]
    updated = {k:v for (k, v) in value.items() if v != 0}
    dic[key] = updated

If you want to make a copy, simply create another dictionary and add it:

new_dict = {}
for key in dic:
    value = dic[key]
    updated = {k:v for (k, v) in value.items() if v != 0}
    new_dict[key] = updated

These approaches can be seen here (commented out)

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.