Re: [Tutor] Using insert method on a list matrix
Raj Medhekar wrote: > Thanks that works! But what if I wanted to modify 2 of the three lists > in 2 different positions? Could you please let me know how I would go > about doing that? M[0].insert(2, 'foo') M[2].insert(1, 'bar') ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using insert method on a list matrix
Thanks that works! But what if I wanted to modify 2 of the three lists in 2 different positions? Could you please let me know how I would go about doing that? Thanks! -Raj From: Ken Oliver To: Raj Medhekar ; Python Tutor Sent: Sunday, July 19, 2009 8:16:11 PM Subject: Re: [Tutor] Using insert method on a list matrix try M[0].insert(0, 'pod') -Original Message- > >From: Raj Medhekar > >Sent: Jul 19, 2009 7:12 PM > >To: Python Tutor > >Subject: [Tutor] Using insert method on a list matrix > > > > >I would like to know how I could use the insert method in a List matrix eg. >for the matrix below > >M=[[1,2,3], [3,2,1], [4,3,2]] > >if wanted to insert the string 'pod' in list [0] before [1] in list [0] in M >using the built in method insert How would I go about doing this? I've tried >may List Comprehension possibilities but none of then worked. Below are some >of the things I tried. > >>>> M=[[1,2,3], [3,2,1], [4,3,2]] >>>> M.insert([1][1], 'pod') > >Traceback (most recent call last): > File "", line 1, in >M.insert([1][1], 'pod') >IndexError: list index out of range >>>> [row[0] for row in M.insert(1, 'pod')] > >Traceback (most recent call last): > File > "", line 1, in >[row[0] for row in M.insert(1, 'pod')] >TypeError: 'NoneType' object is not iterable >>>> M.insert(1,'pod') >>>> M >[[1, 2, 3], 'pod', 'pod', [3, 2, 1], [4, 3, 2]] >>>> M.insert[0](1,'pod') > >Traceback (most recent call last): > File "", line 1, in >M.insert[0](1,'pod') >TypeError: 'builtin_function_or_method' object is unsubscriptable > > >Any help is greatly appreciated! Thanks! > >-Raj > > . ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using insert method on a list matrix
"Raj Medhekar" wrote I would like to know how I could use the insert method in a List matrix OK, A list "matrix" is just a list containing other lists. TThere is nothing special about it, it is just an ordinary list. You could do it in two steps: firstList = M[0] firstList.insert(0,'pod') But it is just as easy to do it directly... eg. for the matrix below M=[[1,2,3], [3,2,1], [4,3,2]] if wanted to insert the string 'pod' in list [0] before [1] in list [0] in M You said it right the first time. You want to insert 'pod' into list[0], ie M[0] So you must call the insert method of that object and specify the index of the insertion there: 0 in your case. M[0].insert(...) M=[[1,2,3], [3,2,1], [4,3,2]] M.insert([1][1], 'pod') This makes no sense since insert() takes a single index because lists (always!) have a single dimension. In M's case it is a list of 3 elements, which elements just happen to be lists too. Also insert does not expect the index to be in brackets. [row[0] for row in M.insert(1, 'pod')] insert() returns None so the loop in this comprehension breaks, But you do get the insert syntax right this time. M should now look like M=[[1,2,3], 'pod', [3,2,1], [4,3,2]] M.insert(1,'pod') M [[1, 2, 3], 'pod', 'pod', [3, 2, 1], [4, 3, 2]] You got it right here too but you are inserting it into M not into the first element of M. M.insert[0](1,'pod') So, one more time, it should be M[0].insert(0,'pod') HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using insert method on a list matrix
try M[0].insert(0, 'pod')-Original Message- From: Raj Medhekar Sent: Jul 19, 2009 7:12 PM To: Python Tutor Subject: [Tutor] Using insert method on a list matrix I would like to know how I could use the insert method in a List matrix eg. for the matrix belowM=[[1,2,3], [3,2,1], [4,3,2]]if wanted to insert the string 'pod' in list [0] before [1] in list [0] in M using the built in method insert How would I go about doing this? I've tried may List Comprehension possibilities but none of then worked. Below are some of the things I tried. >>> M=[[1,2,3], [3,2,1], [4,3,2]]>>> M.insert([1][1], 'pod')Traceback (most recent call last): File "", line 1, in M.insert([1][1], 'pod')IndexError: list index out of range>>> [row[0] for row in M.insert(1, 'pod')]Traceback (most recent call last): File "", line 1, in [row[0] for row in M.insert(1, 'pod')]TypeError: 'NoneType' object is not iterable>>> M.insert(1,'pod')>>> M[[1, 2, 3], 'pod', 'pod', [3, 2, 1], [4, 3, 2]]>>> M.insert[0](1,'pod')Traceback (most recent call last): File "", line 1, in M.insert[0](1,'pod')TypeError: 'builtin_function_or_method' object is unsubscriptableAny help is greatly appreciated! Thanks!-Raj . ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Using insert method on a list matrix
I would like to know how I could use the insert method in a List matrix eg. for the matrix below M=[[1,2,3], [3,2,1], [4,3,2]] if wanted to insert the string 'pod' in list [0] before [1] in list [0] in M using the built in method insert How would I go about doing this? I've tried may List Comprehension possibilities but none of then worked. Below are some of the things I tried. >>> M=[[1,2,3], [3,2,1], [4,3,2]] >>> M.insert([1][1], 'pod') Traceback (most recent call last): File "", line 1, in M.insert([1][1], 'pod') IndexError: list index out of range >>> [row[0] for row in M.insert(1, 'pod')] Traceback (most recent call last): File "", line 1, in [row[0] for row in M.insert(1, 'pod')] TypeError: 'NoneType' object is not iterable >>> M.insert(1,'pod') >>> M [[1, 2, 3], 'pod', 'pod', [3, 2, 1], [4, 3, 2]] >>> M.insert[0](1,'pod') Traceback (most recent call last): File "", line 1, in M.insert[0](1,'pod') TypeError: 'builtin_function_or_method' object is unsubscriptable Any help is greatly appreciated! Thanks! -Raj ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor