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
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
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
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
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
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):
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
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
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,
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
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
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
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
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
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
15 matches
Mail list logo