Can Anyone Help me on this

B

blah

i m trying to reverse the order in which the
strings are stored in list

then pop it into another list

what m i doin worng??

here's the code:

list1 = []
list2 = []
list1.extend('123456789')

print list1

for a in list1:
list2 = list1.pop()

print list2
 
J

jmdeschamps

i m trying to reverse the order in which the
strings are stored in list

then pop it into another list

what m i doin worng??

here's the code:

list1 = []
list2 = []
list1.extend('123456789')

print list1

for a in list1:
list2 = list1.pop()

print list2

well, mant things are happening, so many it's not clear to me what
you're trying to do...
BUT, considering your for statement, since you are poping elements out
of list you are shortening it, so you are only getting as far as
element '5'

If what you want is a reversed copy, you could just append list1
elements to list2, and use the reverse function such as.... list2.append(i)
....
list2.reverse()
list1 ['1', '2', '3', '4', '5', '6', '7', '8', '9']
list2
['9', '8', '7', '6', '5', '4', '3', '2', '1']
 
D

David Wahler

If what you want is a reversed copy, you could just append list1
elements to list2, and use the reverse function such as... list2.append(i)
...
list2.reverse()
list1 ['1', '2', '3', '4', '5', '6', '7', '8', '9']
list2
['9', '8', '7', '6', '5', '4', '3', '2', '1']

It's much clearer and simpler to just do:
list2 = list1[:]
list2.reverse()
This saves all the messing around with append, and has the advantage
that it runs faster (by a factor of 30, on my computer).

-- David
 
M

Mike Meyer

If what you want is a reversed copy, you could just append list1
elements to list2, and use the reverse function such as
... list2.append(i)
...

Don't do this by ahnd - let python do it for you:

list2 = list(list1)

or

list2 = list1[:]

<mike
list2.reverse()
list1 ['1', '2', '3', '4', '5', '6', '7', '8', '9']
list2
['9', '8', '7', '6', '5', '4', '3', '2', '1']
 
P

Peter Hansen

i m trying to reverse the order in which the
strings are stored in list

then pop it into another list

what m i doin worng??

here's the code:

list1 = []
list2 = []
list1.extend('123456789')

How about this instead (Python 2.4 or later):

list2 = list(reversed('123456789'))

-Peter
 
B

blah

Thanks Guys, Wow, i didnt knew that there was alist reverse function.
thx. also. i need a good documentation of the os and sys modules as
well as builtin functions of python.

the standard python documentation doesnt work for me. can u recommend
something?
 
A

Alex Martelli

Thanks Guys, Wow, i didnt knew that there was alist reverse function.
thx. also. i need a good documentation of the os and sys modules as
well as builtin functions of python.

the standard python documentation doesnt work for me. can u recommend
something?

I'm biased, but I'd recommend the book "Python in a Nutshell". My
suggestion: take advantage of the "Safari" online library for 2 weeks'
of free subscription -- make sure you cancel after 13 days if you want
to avoid being billed for the subscription's fee. In those 2 weeks you
get to read for free a few books in the online library -- I'd suggest
sampling the Nutshell, the Cookbook and possibly a couple more...


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top