how to mutate a tuple?

Roy Smith roy at panix.com
Tue Oct 14 09:33:46 EDT 2003


"Carlo v. Dango" <oest at soetu.eu> wrote:
> Hello there. I have a function which as an argument takes a tuple and 
> either returns that tuple or a mutated version of it. The problem is that 
> tuples are imutable, hence I have to create a new tuple and copy the 
> content of the old tuple to a new one.
> 
> But how do I do this if I only at runtime know the size of the tuple?

Copy your input tuple to a list, then say "myTuple = tuple (myList)"

myList = []
for item in myInputTuple:
   if this is the item that needs changing:
      item = something else
   myList.append (item)

return tuple (myList)




More information about the Python-list mailing list