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 ite

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 g

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 Ethan Furman
Neil Cerutti wrote: On 2010-10-21, James Mills 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 to cirrhoss

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

Re: how to scrutch a dict()

2010-10-21 Thread Hrvoje Niksic
Joost Molenaar 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, is not necessary fo

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 Neil Cerutti
On 2010-10-21, James Mills 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 to cirrhossis of the di

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 wrote: > > Not Hyp: > > def _scrunch(**dict): >    result = {}

Re: how to scrutch a dict()

2010-10-21 Thread John Pinner
On Oct 21, 5:40 am, Paul Rubin wrote: > Phlip 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". > >

Re: how to scrutch a dict()

2010-10-20 Thread Ben Finney
Phlip 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 *caller* to decide if t

Re: how to scrutch a dict()

2010-10-20 Thread Chris Rebert
On Wed, Oct 20, 2010 at 9:40 PM, Paul Rubin wrote: > Phlip 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 N

Re: how to scrutch a dict()

2010-10-20 Thread James Mills
On Thu, Oct 21, 2010 at 2: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 an

Re: how to scrutch a dict()

2010-10-20 Thread Paul Rubin
Phlip 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 that? Pyth

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 mana