Can Anyone Help me on this

David Wahler dwahler at gmail.com
Thu Nov 3 19:56:26 CET 2005


jmdeschamps at 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




More information about the Python-list mailing list