On Mon, Aug 25, 2008 at 8:52 PM, Simon Mullis <[EMAIL PROTECTED]> wrote: > Hi All, > > Quick question, I can't seem to find the answer online (well, at the > moment I think the answer is a simple "no" but I would like to > confirm). > > Consider the following hash: > > h = { "1" : "a\r", "2" : "b\n" } > > In order to strip the dict values in Python I (think) I can only do > something like: > > for k,v in h.items: > h[k] = v.strip() > > While in Ruby - for the equivale dict/hash - I have the option of an > in-place method: > > h.each_value { |v| val.strip! } > > Are there Python equivalents to the "!" methods in Ruby? > > The reason I ask is that I have some fairly complex data-structures > and this would make my code alot cleaner... If this is not an accepted > and pythonic way of doing things then please let me know... and I'll > stop! >
how about this one: >>> h = { "1" : "a\r", "2" : "b\n" } >>> dict((k, h[k].strip()) for k in h) {'1': 'a', '2': 'b'} >>> -- Best Regards, Leo Jay -- http://mail.python.org/mailman/listinfo/python-list