473,408 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

how to mutate a tuple?

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 ;)

Am I the first one with a problem like this? I'm not able to find anything
using google on this topic. Hope someone can help me ;)

-Carlo v. Dango
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 18 '05 #1
10 5190
"Carlo v. Dango" <oe**@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)
Jul 18 '05 #2
Carlo v. Dango asks:
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 ;)


Convert *args to a list:
args = list(args)

kwargs should be a dict anyway, not a tuple.

Emile van Sebille
em***@fenx.com
Jul 18 '05 #3
"Carlo v. Dango" <oe**@soetu.eu> wrote in
news:op**************@news.kadnet.dom:
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 ;)


On entry to your function, convert the original tuple to a list, then
mutate the list and convert it back to a tuple when returning.

e.g.
def ChangeIt(aTuple): work = list(aTuple)
work[0] = "Hello"
return tuple(work)
ChangeIt((1,2,3)) ('Hello', 2, 3)

--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #4
On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <ro*@panix.com> wrote:

Many thanks for your quick reply!

aaaahh so there is a tuper() construct! :) that helps a lot.. still this
seems rather inefficient to create a list and then a new tuple.. but I
guess there really is no easier way.

but is there a tuple=>list function ? so that instead of creating the list
by a for-construct I could just say

list = tuple.tolist()
list[0] = newval
return turple(list)

?

-c.v.d.

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)


--
Jul 18 '05 #5
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.
Jul 18 '05 #6

"Carlo v. Dango" <oe**@soetu.eu> wrote in message
news:op**************@news.kadnet.dom...
On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <ro*@panix.com> wrote:

Many thanks for your quick reply!

aaaahh so there is a tuper() construct! :) that helps a lot.. still this
seems rather inefficient to create a list and then a new tuple.. but I
guess there really is no easier way.

but is there a tuple=>list function ? so that instead of creating the list
by a for-construct I could just say

list = tuple.tolist()
list[0] = newval
return turple(list)
No. If you think about it, for such a method to work, it would have
to change the object in place from a tuple to a list, otherwise none
of the bindings would work. If it did it in place, then a tuple
wouldn't be immutable, would it?

If you need to mutate a tuple, then I'd begin to question
whether there isn't something else wrong with the design.

John Roth
?

-c.v.d.

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)


--

Jul 18 '05 #7
On Tue, 14 Oct 2003 10:14:25 -0400, John Roth <ne********@jhrothjr.com>
wrote:

"Carlo v. Dango" <oe**@soetu.eu> wrote in message
news:op**************@news.kadnet.dom...
On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <ro*@panix.com> wrote:

Many thanks for your quick reply!

aaaahh so there is a tuper() construct! :) that helps a lot.. still this
seems rather inefficient to create a list and then a new tuple.. but I
guess there really is no easier way.

but is there a tuple=>list function ? so that instead of creating the
list
by a for-construct I could just say

list = tuple.tolist()
list[0] = newval
return turple(list)
No. If you think about it, for such a method to work, it would have
to change the object in place from a tuple to a list,

sorry, the code should have been: mylist = mytuple.tolist()

If you need to mutate a tuple, then I'd begin to question
whether there isn't something else wrong with the design.


I do.. I'd rather pass around a list.. but as I said I'm using the *args
mechanism in python which is a tuple and not a list.
-Carlo van Dango

Jul 18 '05 #8
On Tue, 14 Oct 2003 23:28:24 +1000, Andrew Bennetts
<an***************@puzzling.org> wrote:

many thanks to you all for all the kind replies.. I'm sorry for posting
such a simple question.. but I wasn't able to read about tuple() and
list() ... without those two its a bit difficult to do what I wanted ;)

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

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


--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 18 '05 #9
> but is there a tuple=>list function ? so that instead of creating the list
by a for-construct I could just say

list = tuple.tolist()
list[0] = newval
return turple(list)


foo = (1,2,3)
foo_list = list(foo)

Diez
Jul 18 '05 #10

"Carlo v. Dango" <oe**@soetu.eu> wrote in message
news:op**************@news.kadnet.dom...
On Tue, 14 Oct 2003 10:14:25 -0400, John Roth <ne********@jhrothjr.com>
wrote:

"Carlo v. Dango" <oe**@soetu.eu> wrote in message
news:op**************@news.kadnet.dom...
On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <ro*@panix.com> wrote:

Many thanks for your quick reply!

aaaahh so there is a tuper() construct! :) that helps a lot.. still this seems rather inefficient to create a list and then a new tuple.. but I
guess there really is no easier way.

but is there a tuple=>list function ? so that instead of creating the
list
by a for-construct I could just say

list = tuple.tolist()
list[0] = newval
return turple(list)
No. If you think about it, for such a method to work, it would have
to change the object in place from a tuple to a list,

sorry, the code should have been: mylist = mytuple.tolist()


Ah. That's not a bad idea. Mostly it's a language
design decision. object.toSomeOtherType() designs put the
responsibility for converting on the 'from' object, languages
that overload their constructors (like Python) put the responsibility
on the 'to' object. Neither one is clearly better since neither one
gracefully handles the question of what to do when you add a new
type: there are always places where you can't easily add the
needed glue.

Python has chosen to go down the path of haveing one obviously
'right' way to do most tasks (as distinct from Perl, whose motto is
"there's more than one way to do it",) and it's chosen to put the
responsibility for type conversion on the constructor of the new type.

John Roth


If you need to mutate a tuple, then I'd begin to question
whether there isn't something else wrong with the design.


I do.. I'd rather pass around a list.. but as I said I'm using the *args
mechanism in python which is a tuple and not a list.
-Carlo van Dango

Jul 18 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Yomanium Yoth Taripoät II | last post by:
HI, 1) what are the differences between list and tuple? 2) how to concatenate tuple and list? no method, no opérator? 3) im looking the fucking manual, and cant add value in my tuple, when it...
50
by: Will McGugan | last post by:
Hi, Why is that a tuple doesnt have the methods 'count' and 'index'? It seems they could be present on a immutable object. I realise its easy enough to convert the tuple to a list and do this,...
1
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can...
15
by: Steve M | last post by:
Hello, I'm trying to figure out the index position of a tuple member. I know the member name, but I need to know the members index position. I know that if I use the statement print tuple that...
37
by: Gregor Horvath | last post by:
Hi, >>>type() <type 'list'> >>>type(('1')) <type 'str'> I wonder why ('1') is no tuple????
77
by: Nick Maclaren | last post by:
Why doesn't the tuple type have an index method? It seems such a bizarre restriction that there must be some reason for it. Yes, I know it's a fairly rare requirement. Regards, Nick...
2
by: Alan Isaac | last post by:
I am probably confused about immutable types. But for now my questions boil down to these two: - what does ``tuple.__init__`` do? - what is the signature of ``tuple.__init__``? These...
4
by: Alex Vinokur | last post by:
Here is some tuple (triple in this case) with uniform types (for instance, double): boost::tuple<double, double, doublet; Is there any way (in boost::tuple) to define such tuples something like...
0
by: Hatem Nassrat | last post by:
on Wed Jun 13 10:17:24 CEST 2007, Diez B. Roggisch deets at nospam.web.de wrote: Well I have looked into this and it seems that using the list comprehension is faster, which is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.