Context:
- Python 3.9
- I have a list
thetas
with 2 values. I initiate this list with some initial values. I intend to update this list in-place for every iteration. - I have another list
theta_history
where I intend to store the value ofthetas
for every iteration usingupdate_thetas
function.
Issue:
theta_history
ends up becoming a list of length = iterations
, but only the final value of thetas
repeated instead of the values from each iteration.
Approach (I am using .append()
, but have also tried using .insert(ind, val)
- with same results)
def update_thetas(thetas):
for i in range(len(thetas)):
thetas[i] = thetas[i] + 1
print(f"thetas: {thetas}")
return thetas
thetas = [0] * 2
thetas[0] = 10
thetas[1] = 5
theta_history = []
iterations = 3
for i in range(iterations):
theta_history.append(thetas)
thetas = update_thetas(thetas)
print(f"theta_history: {theta_history}")
This produces:
thetas: [11, 6]
thetas: [12, 7]
thetas: [13, 8]
theta_history: [[13, 8], [13, 8], [13, 8]]
What I want:
theta_history: [[11, 6], [12, 7], [13, 8]]
Thanks!