how to mutate a tuple?

Andrew Bennetts andrew-pythonlist at puzzling.org
Tue Oct 14 15:28:24 CEST 2003


On Tue, Oct 14, 2003 at 03:22:47PM +0200, Carlo v. Dango 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? I 
> wish I could pass around lists instead.. that would be so much easier, but 
> I'm passing "*args" and "**kwargs" around so I'm not really the one 
> deciding the use of tuples or lists ;)

You could always just convert it to a list, and mutate that, e.g.:

    mutable = list(your_tuple)
    mutate(mutable)
    new_tuple = tuple(mutable)

-Andrew.






More information about the Python-list mailing list