Equivalents of Ruby's "!" methods?

Leo Jay python.leojay at gmail.com
Mon Aug 25 15:03:58 CEST 2008


On Mon, Aug 25, 2008 at 8:52 PM, Simon Mullis <simon at mullis.co.uk> 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



More information about the Python-list mailing list