472,354 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 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 5079
"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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.