473,591 Members | 2,897 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5203
"Carlo v. Dango" <oe**@soetu.e u> 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.e u> wrote in
news:op******** ******@news.kad net.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.u k
int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
"\6\7\xb\1\x9\x a\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.co m> 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.e u> wrote in message
news:op******** ******@news.kad net.dom...
On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <ro*@panix.co m> 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********@jhr othjr.com>
wrote:

"Carlo v. Dango" <oe**@soetu.e u> wrote in message
news:op******** ******@news.kad net.dom...
On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <ro*@panix.co m> 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.or g> 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

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

Similar topics

9
4066
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 already created :/ how to do it? thx.
50
3669
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, I'm just curious why it is neccesary.. Thanks,
1
3247
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 pickle it? Thanks , Fedor
15
4032
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 it will print the contents of that location. What I don't understand is if I know that foo is a member of tuple, how do I get foo's index position. Thanks-in-Advance Steve
37
2369
by: Gregor Horvath | last post by:
Hi, >>>type() <type 'list'> >>>type(('1')) <type 'str'> I wonder why ('1') is no tuple????
77
4390
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 Maclaren.
2
3479
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 questions are stimulated by http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303439 Looking at that, what fails if I leave out the following line? ::
4
2419
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 uniform_tuple <3, doublet; ? Alex Vinokur
0
1784
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 reasonable since generators require iteration and stop iteration and what not.
0
7934
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8236
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7992
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8225
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5732
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5400
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3850
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3891
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.