Newbi Q: Recursively reverse lists but NOT strings?
f.guerrieri at gmail.com
Mon Oct 15 14:47:30 CEST 2007
On 10/15/07, Steven D'Aprano <steve at remove-this-cybersource.com.au> wrote:
> >>> ''.join(reversed("abc"))
> >>> 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))
> return type(sequence)(reversed(sequence))
> (in fact, that's so simple I wonder why the built-in reversed() doesn't
> do that).
In the face of ambiguity, refuse the temptation to guess.
More information about the Python-list