dict.updated
Bengt Richter
bokr at oz.net
Wed Jan 12 22:01:33 EST 2005
On Wed, 12 Jan 2005 23:47:13 GMT, "Rick Morrison" <ram at forefront-tech.No.Lunch.meat.com> wrote:
>I could live with creating a new dict, sure (although it seems wasteful). I
>realize that something like this probably doesn't stand a chance of ever
>making it into the std library for what might be called "philosophical"
>reasons. I just want it for me (my personal philosophy runs more to the
>pragmatic -- well at least for coding).
>
>I suppose the in-place version would be more along the lines of:
>
>>>> def updated(d, updates):
>... d.update(updates)
>... return d
>...
>>>> [updated(d, {'c':3}) for d in [{'a':1, 'b':2}, {'x':10, 'y':'11'}]]
>[{'a': 1, 'c': 3, 'b': 2}, {'y': '11', 'x': 10, 'c': 3}]
>
It's kind of a strange list comp, but I guess you're just illustrating something.
But for this case you could just write
>>> [d.update({'c':3}) or d for d in [{'a':1, 'b':2}, {'x':10, 'y':'11'}]]
[{'a': 1, 'c': 3, 'b': 2}, {'y': '11', 'x': 10, 'c': 3}]
taking advantage of normal d.update() returning None and so forcing the 'or' term.
Regards,
Bengt Richter
More information about the Python-list
mailing list