[Tutor] Problems understanding List reverse()

Josh Gregorio jgregorio@ultrasw.com
Wed, 24 Apr 2002 16:26:19 -0700


I'm a newbie too, so this may not be the right way to do it. I found a
similar example on page 41 of Learning Python. The second to last paragraph
explains string.split (I have the first edition).

# a simple reverse function
import string #import the string module, chapter 3 in learning python
def reversel(list):
    list.reverse()
    return list

alist = []
alist2 = [1, 2, 3, 4, 5]
x = (raw_input('Enter a string:  '))
alist = string.split(x) #take the string and split its stuff into a list
print alist
print reversel(alist)
a = reversel(alist2)
print a

It only works if you enter a string with more than one word, like 'hello my
friend' or something. I'm not sure how to take a one word string like
'hello' and break it up into its individual characters. The following works,
but may not be the best/correct way to do it.

import string
x = 'hello'
alist = []
for i in x:
   alist.append(i)

print x
print alist
alist.reverse()
print alist


Thanks for any comments,

Josh

----- Original Message -----
From: Wesley Abbott <wesleyabbott@hotmail.com>
To: <tutor@python.org>
Sent: Wednesday, April 24, 2002 3:02 PM
Subject: [Tutor] Problems understanding List reverse()


>
> Hello everyody.
>
> I am having a slight problem with relation to 'list.reverse'. I am able ot
> get the reverse to work if a list is static, but if I have a user input
> information it does not seem to abe able to reverse. Please help. Code is
> below. TIA for all your help. I am totally new to programming, coming from
a
> network engineer background. I am going through Wesley Chun's book right
> now. I also have the O'Reilly Learning to program Python. Once again
thanks
> to all who can help me. I have all the different lists jsut to help me
> figure this out. Not trying to confuse anybody, :)
>
> # a simple reverse function
> def reversel(list):
>     list.reverse()
>     return list
>
> alist = []
> alist2 = [1, 2, 3, 4, 5]
> alist.append(raw_input('Enter a string:  '))
> print alist
> print reversel(alist)
> a = reversel(alist2)
> print a
>
> _________________________________________________________________
> Send and receive Hotmail on your mobile device: http://mobile.msn.com
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor