Python - How to change values in a list of lists?
You could use enumerate()
:
for index, sublist in enumerate(execlist):
if sublist[0] == mynumber:
execlist[index][1] = myctype
execlist[index][2] = myx
execlist[index][3] = myy
execlist[index][4] = mydelay
# break
You can remove the #
if execlist
only contains at most one sublist whose first item can equal mynumber
; otherwise, you'll cycle uselessly through the entire rest of the list.
And if the itemnumbers
are in fact unique, you might be better off with a dictionary or at least an OrderedDict
, depending on what else you intend to do with your data.
The problem is that you are creating a copy of the list and then modifying the copy. What you want to do is modify the original list. Try this instead:
for i in range(len(execlist)):
if execlist[i][0] == mynumber:
execlist[i][1] = myctype
execlist[i][2] = myx
execlist[i][3] = myy
execlist[i][4] = mydelay