-4

This is my dictionary

mydict = {'PENNY': 0, "NICKEL": 0, "DIME": 3, "QUARTER": 5, "HALF DOLLAR": 3, "ONE": 0}

It's a dictionary of the change I have in my pocket.

I want come up with way to delete the items from mydict whose values are 0.

I want to change

{'PENNY': 0, "NICKEL": 0, "DIME": 3, "QUARTER": 5, "HALF DOLLAR": 3, "ONE": 0} 

into

{"DIME": 3, "QUARTER": 5, "HALF DOLLAR": 3}

How do I do this?

Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
  • 1
    Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/help/how-to-ask) to learn how to write effective questions. – Morgan Thrapp Dec 08 '15 at 18:46
  • You should check Python documentation for this. – ferrix Dec 08 '15 at 18:48

2 Answers2

0
new_dict={ k:v for k, v in mydict.iteritems() if v != 0 }
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
Suresh Mali
  • 328
  • 1
  • 6
  • 19
0

This should help:

print dict(filter(lambda x:x[1] !=0,ab.iteritems()))

Output:

{'QUARTER': 5, 'HALF DOLLAR': 3, 'DIME': 3}
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57