I am trying to create a custom transform for a list of lists. I am trying to run arithmetic operations on the sublist, but I am not successful. Here is my code:
def my_transform(seq):
new_seq = []
new_seq[0] = seq[0] + 100
new_seq[1] = seq[1] - 1
new_seq[2] = seq[2] * seq[2]
return new_seq
my_list = [[1,2,3], [4,5,6]]
new_list = map(my_transform, my_list) # error
# ideal output:
#[[101,1,9], [104,4,36]]
But I get the following error:
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
map(my_transform,my_list)
File "<pyshell#7>", line 3, in my_transform
new_seq[0] = seq[0] + 100
IndexError: list assignment index out of range
I also tried:
[my_transform(L) for L in my_list]
That gave me the same error, too. How can I get the results using map and/or list comprehension? Thanks!
Paul