473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

dict.updated

Would there be any way to add a method to all dict objects that operated
like the .update() method, but also returned a reference to the updated
dict?

..update() is clumsy to use inside of list comprehensions and the like. Or am
I missing something?

Thanks,
Rick


Jul 18 '05 #1
6 1744
Rick Morrison wrote:
Would there be any way to add a method to all dict objects that operated
like the .update() method, but also returned a reference to the updated
dict?


Are you looking for updated() to parallel sorted(), where sorted()
returns a *new* list? I doubt you'll be able to rally much support for
a method that changes an object and returns a reference to it -- check
the archives to see these kind of things getting rejected over and over.

Could you do something like:

py> import itertools
py> d1 = dict(a=1, b=2)
py> d2 = dict(c=3, d=4)
py> d3 = dict(itertools.chain(d1.iteritems(), d2.iteritems()))
py> d3
{'a': 1, 'c': 3, 'b': 2, 'd': 4}

Steve
Jul 18 '05 #2
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}]

But I'd like to put the "updated" method on the "dict" object, which is what
I can't seem to figure out.
Yeah I know that's "bad", but to my mind so is polluting the global
namespace with the "updated" function.

-- Or maybe I'm just lazy and picked up too many bad habits from
Javascript.

Rick
"Steven Bethard" <st************@gmail.com> wrote in message
news:dv********************@comcast.com... Rick Morrison wrote:
Would there be any way to add a method to all dict objects that operated
like the .update() method, but also returned a reference to the updated
dict?


Are you looking for updated() to parallel sorted(), where sorted()
returns a *new* list? I doubt you'll be able to rally much support for
a method that changes an object and returns a reference to it -- check
the archives to see these kind of things getting rejected over and over.

Could you do something like:

py> import itertools
py> d1 = dict(a=1, b=2)
py> d2 = dict(c=3, d=4)
py> d3 = dict(itertools.chain(d1.iteritems(), d2.iteritems()))
py> d3
{'a': 1, 'c': 3, 'b': 2, 'd': 4}

Steve

Jul 18 '05 #3
On Wed, 12 Jan 2005 23:47:13 GMT, Rick Morrison
<ra*@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}]

But I'd like to put the "updated" method on the "dict" object, which is what
I can't seem to figure out.
Yeah I know that's "bad", but to my mind so is polluting the global
namespace with the "updated" function.


[dict(d.items() + {'c':3}.items()) for d in [{'a':1, 'b':2}, {'x':10,
'y':'11'}]]
seems logical enough to me....
-- Or maybe I'm just lazy and picked up too many bad habits from
Javascript.


probably.

Stephen.
Jul 18 '05 #4
Rick Morrison 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}]

But I'd like to put the "updated" method on the "dict" object, which is what
I can't seem to figure out.
Yeah I know that's "bad", but to my mind so is polluting the global
namespace with the "updated" function.


You could do something like:

py> class dict(dict):
.... def updated(self, *args, **kwds):
.... self.update(*args, **kwds)
.... return self
....
py> [d.updated(c=3) for d in [dict(a=1, b=2), dict(x=10, y=11)]]
[{'a': 1, 'c': 3, 'b': 2}, {'y': 11, 'x': 10, 'c': 3}]

It'd mean you'd have to create all your dicts with the dict constructor
instead of {} though.

Steve
Jul 18 '05 #5

Rick Morrison wrote:
[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}]


I don't really understand the use of this. Can you give a less toy
example? I'd probably just do

dicts = [{'a':1, 'b':2}, {'x':10, 'y':'11'}]
for d in dicts: d.update({'c':3})

This isn't really more typing or any clumsier IMO, so I can't see why
you'd want to use list comprehensions in this case.

Jul 18 '05 #6
On Wed, 12 Jan 2005 23:47:13 GMT, "Rick Morrison" <ra*@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
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Robin Cull | last post by:
Imagine I have a dict looking something like this: myDict = {"key 1": , "key 2": , "key 3": , "key 4": } That is, a set of keys which have a variable length list of associated values after...
12
by: Christopher J. Bottaro | last post by:
If I have the following class: class MyClass: def __init__(self): m_dict = {} m_dict = 1 m_dict = 2 m_dict = 3 Is there anyway to generate automatic accessors to the elements of the dict?
2
by: Joh | last post by:
Hello, (first i'm sorry for my bad english...) for a program, i need to have some kind of dictionary which will contain a huge amount of keys and values. i have tried 'to do it simple' using...
3
by: Bengt Richter | last post by:
Has anyone found a way besides not deriving from dict? Shouldn't there be a way? TIA (need this for what I hope is an improvement on the Larosa/Foord OrderedDict ;-) I guess I can just document...
11
by: sandravandale | last post by:
I can think of several messy ways of making a dict that sets a flag if it's been altered, but I have a hunch that experienced python programmers would probably have an easier (well maybe more...
15
by: Cruella DeVille | last post by:
I'm trying to implement a bookmark-url program, which accepts user input and puts the strings in a dictionary. Somehow I'm not able to iterate myDictionary of type Dict{} When I write print...
15
by: George Sakkis | last post by:
Although I consider dict(**kwds) as one of the few unfortunate design choices in python since it prevents the future addition of useful keyword arguments (e.g a default value or an orderby...
12
by: jeremito | last post by:
Please excuse me if this is obvious to others, but I can't figure it out. I am subclassing dict, but want to prevent direct changing of some key/value pairs. For this I thought I should override...
1
by: | last post by:
Hello all, I have a question which might be simple or need some work around. I want to do something like this. My class/instance has a dict as a property. I want the instance to catch the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.