Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - How to change values in a list of lists?

I have a list of lists, each list within the list contains 5 items, how do I change the values of the items in the list? I have tried the following:

    for [itemnumber, ctype, x, y, delay] in execlist:
        if itemnumber == mynumber:
            ctype = myctype
            x = myx
            y = myy
            delay = mydelay

Originally I had a list of tuples but I realized I cant change values in a tuple so I switched to lists but I still cant change any of the values. If I print ctype, x, y, delay, myctype, myx, myy, or mydelay from within the for loop it appears that everything is working but if I print execlist afterwards I see that nothing has changed.

like image 372
Wichid Nixin Avatar asked Dec 06 '12 20:12

Wichid Nixin


1 Answers

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
like image 119
Jeff Silverman Avatar answered Oct 06 '22 08:10

Jeff Silverman