Reversing a string
aahz at pythoncraft.com
Wed Jul 4 17:57:48 CEST 2007
In article <Xns99616C389BA94martinwilliamsdurkin at 126.96.36.199>,
Martin Durkin <nospam at williamsdurkin.co.uk> wrote:
>aleax at mac.com (Alex Martelli) wrote in
>news:1i0kjpx.wjw44j15np22dN%aleax at mac.com:
>> So, something like:
>> for c in reversed(x): print c
>> is mostly likely how I'd present the solution to the task.
>This is an interesting point to me. I am just learning Python and
>I wonder how I would know that a built in function already exists?
>At what point do I stop searching for a ready made solution to a
>particular problem and start programming my own function? Is it just a
>matter of reading *all* the documentation before I start coding?
You should at least know that you can do:
l = list(s)
for c in l:
This works in all versions of Python back to 1.5.2 IIRC. reversed() is
a moderately new built-in function; I would agree with people who claim
that you should memorize most of the built-in functions (which is
precisely why there is a high barrier to adding more built-in functions).
But certainly if you're using a datatype you should make a point of
reading all its documentation, which would mean you'd know that list()
can convert any iterable type.
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
I support the RKAB
More information about the Python-list