invert or reverse a string... warning this is a rant
Neil Cerutti
horpner at yahoo.com
Thu Oct 19 12:45:54 EDT 2006
On 2006-10-19, Brad <rtilley at vt.edu> wrote:
> I'm home for lunch so my email addy is different.
>
> No, it doesn't happen very often, but when I need to reverse
> something (usually a list or a string). I can never remember
> right of the top of my head how to do so in Python. I always
> have to Google for an answer or refer back to old code.
>
> IMO, I should be able to intuitively know how to do this.
> Python is so user-friendly most every place else... why can it
> not be so here?
>
> I wrote this so I'll never have to remember this again:
>
> def invert(invertable_object):
> try:
> print invertable_object[::-1]
> return invertable_object[::-1]
> except:
> print 'Object not invertable'
> return 1
>
> invert([1,2,3,4])
> invert('string')
> invert({1:2, 3:4})
Shoot, now you'll have to remember where in heck you stashed that
function the next time you need to reverse something. ;-)
You'll still be better off in the long run memorizing the slice
notation.
--
Neil Cerutti
More information about the Python-list
mailing list