On 6/30/10 8:50 PM, Mladen Gogala wrote:
x="quick brown fox jumps over a lazy dog"
y=''.join(list(x).reverse())
Traceback (most recent call last):
   File "<stdin>", line 1, in<module>
TypeError



Why is TypeError being thrown? The reason for throwing the type error is
the fact that the internal expression evaluates to None and cannot,
therefore, be joined:

The "reverse" method, like "sort" and a couple others, are in-place operations. Meaning, they do not return a new list but modify the existing list. All methods that are "in-place" modifications return None to indicate this. This way you can't make a mistake and think its returning a sorted / reversed copy but it isn't.

However, you can easily get what you want by using the 'reversed' function (and similarly, the 'sorted' function), a la:

>>> y = ''.join(reversed(list(x)))

The 'reversed' and 'sorted' functions are generators that lazilly convert an iterable as needed.

--

   ... Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to