Newbi Q: Recursively reverse lists but NOT strings?
Francesco Guerrieri
f.guerrieri at gmail.com
Mon Oct 15 08:47:30 EDT 2007
On 10/15/07, Steven D'Aprano <steve at remove-this-cybersource.com.au> wrote:
> >>> ''.join(reversed("abc"))
> 'cba'
> >>> list(reversed(range(3)))
> [2, 1, 0]
>
> It doesn't take much to make a more user-friendly version:
>
>
> def myreversed(sequence):
> if isinstance(sequence, basestring):
> return type(sequence)().join(reversed(sequence))
> else:
> return type(sequence)(reversed(sequence))
>
> (in fact, that's so simple I wonder why the built-in reversed() doesn't
> do that).
simple:
In the face of ambiguity, refuse the temptation to guess.
:-)
More information about the Python-list
mailing list