Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Appending to a certain index in a list (http://www.programmingforums.org/showthread.php?t=9627)

commodore May 2nd, 2006 2:41 PM

Appending to a certain index in a list
 
I have a list with 5 elements. I want to change the 3rd element and to move the elements forward (3>4, 4>5, 5>6) at the same time. Is there a simple way to do this or do I have to do something mad?

I'm sorry if this is stupid, but I'm a beginner.

andro May 2nd, 2006 2:49 PM

I think it's as easy as

:

list.insert(i, x)

Arevos May 2nd, 2006 3:34 PM

What andro says.
:

list = range(5)    # create a list of 5 elements, from 0 to 4
list.insert(2, 9)  # change the 3rd element 9 and move the rest forward
# (Indicies start from 0, so the index of 2 refers to the 3rd element)


Sane May 2nd, 2006 3:37 PM

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.

commodore May 3rd, 2006 7:19 AM

Thanks everyone! The python sub-forum seems a lot friendlier than the HTML forum :)

Sane that's a great tip!

hydroxide May 8th, 2006 10:25 AM

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)

Dietrich May 8th, 2006 11:13 AM

hydroxide, you are right
:

# this inserts 'new' at index 3
my_list = ['a', 'b', 'c', 'd', 'e']
my_list[3:3] = ['new']
print my_list  # ['a', 'b', 'c', 'new', 'd', 'e']


Sane May 8th, 2006 3:34 PM

Ah, that's interesting. Very abstract sort of logic, would have never though of that to work.

Dietrich May 8th, 2006 11:36 PM

Actually I found it in one of the Python Reference Manuals:
Quote:

s.insert(i, x) same as s[i:i] = [x] if i >= 0
also i == -1 inserts before the last element
Didn't give it much attention until hydroxide mentioned it.


All times are GMT -5. The time now is 6:41 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC