Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I make my output exclude an element if it equals to zero?

So I created this code with the help of Stack Overflow users.

def get_name(string):
    return string.replace("+", "").replace("-", "")

def gnames(input_list: list):
    output = {}
    for entry in input_list:
        if '->' in entry:
            names = entry.split('->')
            output[names[1]] = output[names[0]]
            output[names[0]] = 0
        else:
            name = get_name(entry)
            if name not in output:
                output[name] = 0
            if "++" in entry:
                output[name] += 1
            if "--" in entry:
                output[name] -= 1
    return output
print(gnames(["Jim--", "John--", "Jordan--", "Jim++", "John--", "Jeff--", "June++", "June->Jim"]))

and this returns

{'Jim': 1, 'John': -2, 'Jordan': -1, 'Jeff': -1, 'June': 0}

Now this is right, but I want gnames() to return only the non zero values negative numbers or positive numbers are fine

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

so in my example, there’s 'June' = 0

and I want the output of gnames() to exclude 'June' = 0 or if any other person has a 0… I want gnames() to exclude it…

so my output in thiscase, should return

{'Jim': 1, 'John': -2, 'Jordan': -1, 'Jeff': -1}

How can I do that??

>Solution :

Also using dictionary comprehension, you can alter your return like so:

return {x:output[x] for x in output if output[x] != 0}

Description of what this does:

# for each element in output
#    if element['name'] != 0 then keep it

Full code:

def get_name(string):
    return string.replace("+", "").replace("-", "")

def gnames(input_list: list):
    output = {}
    for entry in input_list:
        if '->' in entry:
            names = entry.split('->')
            output[names[1]] = output[names[0]]
            output[names[0]] = 0
        else:
            name = get_name(entry)
            if name not in output:
                output[name] = 0
            if "++" in entry:
                output[name] += 1
            if "--" in entry:
                output[name] -= 1
    return {x:output[x] for x in output if output[x] != 0}


print(gnames(["Jim--", "John--", "Jordan--", "Jim++", "John--", "Jeff--", "June++", "June->Jim"]))

# Sample output
# {'Jim': 1, 'John': -2, 'Jordan': -1, 'Jeff': -1}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading