[Tutor] Using insert method on a list matrix
Raj Medhekar
cosmicsand27 at yahoo.com
Mon Jul 20 01:12:50 CEST 2009
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 "<pyshell#19>", line 1, in <module>
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 "<pyshell#20>", line 1, in <module>
[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 "<pyshell#23>", line 1, in <module>
M.insert[0](1,'pod')
TypeError: 'builtin_function_or_method' object is unsubscriptable
Any help is greatly appreciated! Thanks!
-Raj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090719/725e1e1a/attachment.htm>
More information about the Tutor
mailing list