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
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
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
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
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
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
> 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
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
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 = {}
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".
> >
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
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
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
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
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
15 matches
Mail list logo