View Single Post
Old May 8th, 2006, 11:25 AM   #6
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
Quote:
Originally Posted by Sane
Also, it may be a good idea to become familiar with list indexing. Yes, Andro's code works perfectly for what you need to do. However, there will be times when you need to use an alternative method (for other purposes, eg. replacing the third element), list indexing...

my_list = ['a', 'b', 'c', 'd', 'e']
new_list = my_list[:3] + ['new'] + my_list[3:] 
# essentially: ['a', 'b', 'c'] + ['new'] + ['d', 'e']
print new_list

Any questions, feel free to ask.
IIRC for replacement you can also do "mylist[startrange:endrange] = somelist", eg: "mylist[3:4] = ['new']", making startrange and endrange identical if you want insertion.

-T. (no, I can't test it ;-P)
hydroxide is offline   Reply With Quote