Update a list in python

Hi everyone,

I’m writing code that allows me to subtract the last item, which is not equal to the other items, from each sublist of my original list. Additionally, I want to update the original list within the main loop since it is related to a condition. I am aware that I can update it later using the code original_lst = original_lst[:-1]…so I can create the substract list but I can’t update the original list and I’m getting this error

here my code:

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *


lst1= [37.39, 37.39, 27.28]

original_lst = []
for i in range(0,10):
    temp = []
    for j in lst1:
        temp.append(j)
    original_lst.append(temp)
    
substract_list = []
for i in range(0, len(original_lst)):
    for j in original_lst[i]:
        if j != 37.39:
            substract_list.append(j)
            original_lst.remove(j)
            
OUT = substract_list, original_lst

Any help would be apreciated

Thanks.

You’re getting an item from the sublist and then trying to remove it from the top level list, where it does not exist. Instead of using indices to track sublists and subitems you can use a variable to make things easier (in my opinion). But really you just need to make sure you are removing the item from the sublist.

2 Likes

@Nick_Boyts

I was focusing on removing item from the top-level list, but as you said, the easier way is to remove it from the sublists…Oh my bad :crazy_face:

Thanks.