More pythonic way to change an element of a tuple?

Dave Brueck dave at pythonapocrypha.com
Wed Nov 27 14:34:08 EST 2002


On Wed, 27 Nov 2002, Richard Muller wrote:

> I frequently have to change a single element of a tuple, and I wind up 
> doing something like this:
> 
> tmp = list(data_in_tuple)
> tmp[item] += 1
> data_in_tuple = tuple(tmp)
> 
> Is there a more beautiful way of doing this? Seems a bit awkward.

Is there a reason why the data is in a tuple? Why not just always leave it 
in a list? Tuples are meant to be immutable, so if you find yourself 
mutating them, maybe the best best is simply not use them.

-Dave





More information about the Python-list mailing list