440,772 Members | 906 Online
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

# Can Anyone Help me on this

 P: n/a 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 -- * Posted with NewsLeecher v3.0 Beta 7 * http://www.newsleecher.com/?usenet Nov 3 '05 #1
9 Replies

 P: n/a bl**@blah.blah wrote: 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 -- * Posted with NewsLeecher v3.0 Beta 7 * http://www.newsleecher.com/?usenet 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 ... for i in list1: .... 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'] Nov 3 '05 #2

 P: n/a Here is an example of copying then reversing a list: l1 = [1,2,"C"] l2 = l1[:] l2.reverse() l2 ['C', 2, 1] Nov 3 '05 #3

 P: n/a jm*********@gmail.com wrote: If what you want is a reversed copy, you could just append list1 elements to list2, and use the reverse function such as ... for i in list1: ... 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 Nov 3 '05 #4

 P: n/a jm*********@gmail.com writes: If what you want is a reversed copy, you could just append list1 elements to list2, and use the reverse function such as ... for i in list1: ... list2.append(i) ... Don't do this by ahnd - let python do it for you: list2 = list(list1) or list2 = list1[:] http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. Nov 3 '05 #5

 P: n/a .... list2=list1[:] .... Yes, naturally, where was my head ?-)) Nov 3 '05 #6

 P: n/a bl**@blah.blah wrote: 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 Nov 3 '05 #7

 P: n/a 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? -- * Posted with NewsLeecher v3.0 Beta 7 * http://www.newsleecher.com/?usenet Nov 4 '05 #8

 P: n/a bl**@blah.blah wrote: 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 Nov 4 '05 #9

 P: n/a Thx Alex. -- * Posted with NewsLeecher v3.0 Beta 7 * http://www.newsleecher.com/?usenet Nov 4 '05 #10

### This discussion thread is closed

Replies have been disabled for this discussion.