To clarify my point: <br>reverse()  is  a lucky one  - Python has variants of *this particular* function both  for lists and strings. Yet what about other list functions? How in general, can I write a function that works  both on list and string  types? Both are sequences, right? Why string is not a subtype of a list then?
<br>The advantage of string being a list of elements, where element is a char is that all list functions will work *without any modifications* on strings as well as on other types of lists.<br>So, I am trying to understand: what is a rational for strings not being lists in Python?
<br><br>Thanks,<br>-- <br>Dmitri O. Kondratiev<br><a href="mailto:dokondr@gmail.com">dokondr@gmail.com</a><br><a href="http://www.geocities.com/dkondr">http://www.geocities.com/dkondr</a><br><br><div><span class="gmail_quote">
On 10/15/07, <b class="gmail_sendername">Dmitri O.Kondratiev</b> <<a href="mailto:dokondr@gmail.com">dokondr@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Gary, thanks for lots of info!<br>Python strings are not lists! I got it now. That's a pity, I need two different functions: one to reverse a list and one to reverse a string:<br><br>def reverseList(xs):<br>    if xs == []:
<br>        return xs<br>    else:<br>        return (reverseList (xs[1:])) + [xs[0]]<br><br>def reverseStr(str):<br>    if str == "":<br>        return str<br>    else:<br>        return (reverseStr (str[1:])) + str[0]
<br><br>Ok. Now regarding in-place reversal of a list:<br><br>>>> l = [1,2,3]<br>>>> l<br>[1, 2, 3]<br>>>> l.reverse()<br>>>> l<br>[3, 2, 1]<br><br>That was, as I expected. Good.<br><br>

Then why this ? :<br><br>>>> ls = [1,2,3].reverse()<br>>>> ls<br>>>><br>>>> print [1,2,3].reverse()<br>None<br>>>> <br>I mean, why ls is empty after assignment?<br><br>Also, I couldn't find in the Python docs what this form of slicing means:
<br>xs[::-1]  ?<br><br>It works for creating a reversed copy of either a string or a list, but what does '::-1' syntax means?<br><br>Thanks,<br><span class="sg"><br>Dmitri O. Kondratiev<br><a href="mailto:dokondr@gmail.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
dokondr@gmail.com
</a><br><a href="http://www.geocities.com/dkondr" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://www.geocities.com/dkondr</a></span><div><span class="e" id="q_115a307671eaa129_2"><br><br><div>
<span class="gmail_quote">On 10/15/07, <b class="gmail_sendername">Gary Herron</b> <<a href="mailto:gherron@islandtraining.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
gherron@islandtraining.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Dmitri O.Kondratiev wrote:<br>><br>> The function I wrote (below) reverses lists all right:
<br>><br>> def reverse(xs):<br>>     if xs == []:<br>>         return []<br>>     else:<br>>         return (reverse (xs[1:])) + [xs[0]]<br>><br>><br>> >>> reverse ([1,2,3])<br>> [3, 2, 1]
<br>> >>><br>><br>><br>> Yet when I try to reverse a string I  get:<br>><br>> >>> reverse ("abc")<br>><br>> ...<br>> ...<br>> ...<br>><br>>   File "C:\wks\python-wks\reverse.py", line 5, in reverse
<br>><br>>     return (reverse (xs[1:])) + [xs[0]]<br>><br>>   File "C:\wks\python-wks\reverse.py", line 5, in reverse<br>><br>>     return (reverse (xs[1:])) + [xs[0]]<br>><br>>   File "C:\wks\python-wks\reverse.py", line 2, in reverse
<br>><br>>     if xs == []:<br>><br>> RuntimeError: maximum recursion depth exceeded in cmp<br>><br>> >>><br>><br>> What's wrong? Why recursion never stops?<br>><br>If you are doing this as an python-learning exercise, then read on.   If
<br>you are doing this reversal for real code, then try:<br><br>  xs.reverse() for in-place reversal of a list (but not a string), or<br>  result = xs[::-1] for creating a reversed copy of either a string or a<br>list<br>

<br><br>Your recursion stops when xs == [], but when you're stripping characters<br>off a string,  like 'abc', the remaining portion will be 'bc', then 'c',<br>than '', but never [] so you 'll never stop.
<br><br>Try:<br><br>if xs == []:<br>    return []<br>elif xs == '':<br>    return ''<br>else:<br>    ...<br><br><br>Gary Herron<br><br><br>><br>> Thanks,<br>> Dima<br><br></blockquote></div><br><br clear="all">

<br>
</span></div></blockquote></div><br><br clear="all"><br><br>