Re: how to scrutch a dict()

2010-10-22 Thread Ethan Furman
Neil Cerutti wrote: On 2010-10-21, James Mills prolo...@shortcircuit.net.au wrote: Rather than creating a new dict why don't you just do: def _scrunch(d): for k, v in d.items(): if v is None: del d[k] In Python 3, where items returns an iterator, modifying the dictionary in

Re: how to scrutch a dict()

2010-10-22 Thread Ethan Furman
John Nagle wrote: On 10/20/2010 9:32 PM, Phlip wrote: Not Hyp: def _scrunch(**dict): result = {} for key, value in dict.items(): if value is not None: result[key] = value return result That says throw away every item in a dict if the Value is None. Are there any

Re: how to scrutch a dict()

2010-10-22 Thread John Nagle
On 10/22/2010 6:10 AM, Ethan Furman wrote: John Nagle wrote: class nonnulldict(dict) : def __setitem__(self, k, v) : if not (v is None) : dict.__setitem__(self, k, v) That creates a subclass of dict which ignores stores of None values. So you never store the unwanted items at all. It's

Re: how to scrutch a dict()

2010-10-22 Thread Ethan Furman
John Nagle wrote: On 10/22/2010 6:10 AM, Ethan Furman wrote: John Nagle wrote: class nonnulldict(dict) : def __setitem__(self, k, v) : if not (v is None) : dict.__setitem__(self, k, v) That creates a subclass of dict which ignores stores of None values. So you never store the unwanted

Re: how to scrutch a dict()

2010-10-21 Thread John Pinner
On Oct 21, 5:40 am, Paul Rubin no.em...@nospam.invalid wrote: Phlip phlip2...@gmail.com writes: def _scrunch(**dict):     result = {}     for key, value in dict.items():         if value is not None:  result[key] = value     return result That says throw away every item in a dict

Re: how to scrutch a dict()

2010-10-21 Thread Joost Molenaar
Using a 2.7/3.x dictionary comprehension, since you don't seem to mind creating a new dictionary: def _scrunched(d):     return { key: value for (key, value) in d.items() if value is not None } Joost On 21 October 2010 06:32, Phlip phlip2...@gmail.com wrote: Not Hyp: def _scrunch(**dict):  

Re: how to scrutch a dict()

2010-10-21 Thread Neil Cerutti
On 2010-10-21, James Mills prolo...@shortcircuit.net.au wrote: Rather than creating a new dict why don't you just do: def _scrunch(d): for k, v in d.items(): if v is None: del d[k] In Python 3, where items returns an iterator, modifying the dictionary in this way may lead

Re: how to scrutch a dict()

2010-10-21 Thread Phlip
for k in [k for k, v in d.items() if v is None]:   del d[k] Tx everyone! And I forgot about shadowing dict(), I forgot about del d[k], and I didn't know Python had dict comprehensions yet. Anyway this one might become the winner. -- http://mail.python.org/mailman/listinfo/python-list

Re: how to scrutch a dict()

2010-10-21 Thread Hrvoje Niksic
Joost Molenaar j.j.molen...@gmail.com writes: Using a 2.7/3.x dictionary comprehension, since you don't seem to mind creating a new dictionary: def _scrunched(d):     return { key: value for (key, value) in d.items() if value is not None } Note that a dict comprehension, while convenient,

Re: how to scrutch a dict()

2010-10-21 Thread John Nagle
On 10/20/2010 9:32 PM, Phlip wrote: Not Hyp: def _scrunch(**dict): result = {} for key, value in dict.items(): if value is not None: result[key] = value return result That says throw away every item in a dict if the Value is None. Are there any tighter or smarmier

how to scrutch a dict()

2010-10-20 Thread Phlip
Not Hyp: def _scrunch(**dict): result = {} for key, value in dict.items(): if value is not None: result[key] = value return result That says throw away every item in a dict if the Value is None. Are there any tighter or smarmier ways to do that? Python does so often

Re: how to scrutch a dict()

2010-10-20 Thread Paul Rubin
Phlip phlip2...@gmail.com writes: def _scrunch(**dict): result = {} for key, value in dict.items(): if value is not None: result[key] = value return result That says throw away every item in a dict if the Value is None. Are there any tighter or smarmier ways to do

Re: how to scrutch a dict()

2010-10-20 Thread James Mills
On Thu, Oct 21, 2010 at 2:32 PM, Phlip phlip2...@gmail.com wrote: Not Hyp: def _scrunch(**dict):    result = {}    for key, value in dict.items():        if value is not None:  result[key] = value    return result That says throw away every item in a dict if the Value is None. Are

Re: how to scrutch a dict()

2010-10-20 Thread Chris Rebert
On Wed, Oct 20, 2010 at 9:40 PM, Paul Rubin no.em...@nospam.invalid wrote: Phlip phlip2...@gmail.com writes: def _scrunch(**dict):     result = {}     for key, value in dict.items():         if value is not None:  result[key] = value     return result That says throw away every item in a

Re: how to scrutch a dict()

2010-10-20 Thread Ben Finney
Phlip phlip2...@gmail.com writes: Not Hyp: I don't know what this means; I hope it's not important. def _scrunch(**dict): You're clobbering the built-in ‘dict’ binding here. You're also requiring the input to be keyword parameters. Why not simply take a single parameter, and allow the