Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 2nd, 2006, 3:41 PM   #1
commodore
Programmer
 
Join Date: Nov 2005
Location: Estonia
Posts: 97
Rep Power: 0 commodore is an unknown quantity at this point
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.
commodore is offline   Reply With Quote
Old May 2nd, 2006, 3:49 PM   #2
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 316
Rep Power: 4 andro is on a distinguished road
Send a message via AIM to andro
I think it's as easy as

list.insert(i, x)
andro is offline   Reply With Quote
Old May 2nd, 2006, 4:34 PM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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)
Arevos is offline   Reply With Quote
Old May 2nd, 2006, 4:37 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,013
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to 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.
Sane is offline   Reply With Quote
Old May 3rd, 2006, 8:19 AM   #5
commodore
Programmer
 
Join Date: Nov 2005
Location: Estonia
Posts: 97
Rep Power: 0 commodore is an unknown quantity at this point
Thanks everyone! The python sub-forum seems a lot friendlier than the HTML forum

Sane that's a great tip!
commodore is offline   Reply With Quote
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
Old May 8th, 2006, 12:13 PM   #7
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

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']
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old May 8th, 2006, 4:34 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,013
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Ah, that's interesting. Very abstract sort of logic, would have never though of that to work.
Sane is offline   Reply With Quote
Old May 9th, 2006, 12:36 AM   #9
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

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.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:42 AM.

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