Skip to content
Advertisement

Can’t replace an array inside a tuple with another array of same size – Python

I have a list of tuples:

enter image description here

I want to replace the second np.array of the first tuple with an array of the same length but filled with zeros:

I have tried the following:

Coeff[0][1] =np.zeros(len(Coeff[0][1]))

But I get the:

'tuple' object does not support item assignment

Any idea how to complete the replacement process ?

Advertisement

Answer

Tuples are immutable in Python, you cannot change values stored in it.

You can do list(map(list, list_of_tuples)) for a quick casting into list, or [list(x) for x in list_of_tuples] if you want to use list comprehension.

I’ve done some ugly %timeit benchmark on my computer, both seems merely equivalent in speed.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement