Mar-01-2020, 05:24 PM
Suppose I have the following code
Thanks
newList = [] for i in range(10): newList.append([]) for i in range(len(newList)): newList[i].append(1) >>> newList[0] [1]If however instead of having a list of lists, I have a list of objects containing a list attribute, the output is not the one I would expect:
class testobj: def __init__(tst,lst = []): tst.lst = lst newList = [] for i in range(10): newList.append(testobj()) for i in range(len(newList)): newList[i].lst.append(1) >>> newList[0].lst [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]Can someone explain why? How would I make the list of objects behave correctly?
Thanks