On Aug 25, 7:52 am, "Simon Mullis" <[EMAIL PROTECTED]> wrote: > Consider the following hash: > > h = { "1" : "a\r", "2" : "b\n" }
In Python, this called a "dict". "hash" sounds so... perlish. > > 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? > You have pretty much answered your own question. "!" methods are not specially named or annotated in Python, str.strip is the method that corresponds to strip!. It looks a little different since you left off the ()'s in the Ruby example (as is your privilege and right in Ruby). Pythonistically speaking, even though a dict is a mutable thing, I'm learning that the accepted practice for methods like this is not so much to update in place as it is to use generator expressions to construct a new object. For example, given a list of integers to 100, instead of removing all of the even numbers (with the attendant hassles of updating a list while iterating over it), just create a new list of the numbers that are not even. In your dict case, this would just be: h = dict( k,v.strip() for k,v in h.iteritems() ) Perhaps I am overgeneralizing from comments I have read here on c.l.py about creating new lists instead updating old ones. But in your loop, you *will* end up doing a re-assignment of every value in the dict, even if the original value had no whitespace to strip. -- Paul -- http://mail.python.org/mailman/listinfo/python-list