how to mutate a tuple?

Discussion in 'Python' started by Carlo v. Dango, Oct 14, 2003.

  1. 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/
     
    Carlo v. Dango, Oct 14, 2003
    #1
    1. Advertising

  2. 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.
     
    Andrew Bennetts, Oct 14, 2003
    #2
    1. Advertising

  3. Carlo v. Dango

    Roy Smith Guest

    "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?


    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)
     
    Roy Smith, Oct 14, 2003
    #3
  4. Carlo v. Dango

    Duncan Booth Guest

    "Carlo v. Dango" <> wrote in
    news:eek::

    > 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
    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?
     
    Duncan Booth, Oct 14, 2003
    #4
  5. 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
     
    Emile van Sebille, Oct 14, 2003
    #5
  6. On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <> 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)




    --
     
    Carlo v. Dango, Oct 14, 2003
    #6
  7. Carlo v. Dango

    John Roth Guest

    "Carlo v. Dango" <> wrote in message
    news:eek:...
    > On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <> 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)

    >
    >
    >
    > --
     
    John Roth, Oct 14, 2003
    #7
  8. On Tue, 14 Oct 2003 10:14:25 -0400, John Roth <>
    wrote:

    >
    > "Carlo v. Dango" <> wrote in message
    > news:eek:...
    >> On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <> 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
     
    Carlo v. Dango, Oct 14, 2003
    #8
  9. On Tue, 14 Oct 2003 23:28:24 +1000, Andrew Bennetts
    <> 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/
     
    Carlo v. Dango, Oct 14, 2003
    #9
  10. > 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
     
    Diez B. Roggisch, Oct 14, 2003
    #10
  11. Carlo v. Dango

    John Roth Guest

    "Carlo v. Dango" <> wrote in message
    news:eek:...
    > On Tue, 14 Oct 2003 10:14:25 -0400, John Roth <>
    > wrote:
    >
    > >
    > > "Carlo v. Dango" <> wrote in message
    > > news:eek:...
    > >> On Tue, 14 Oct 2003 09:33:46 -0400, Roy Smith <> 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
    >
     
    John Roth, Oct 14, 2003
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mike p.
    Replies:
    1
    Views:
    331
    Terry Reedy
    Feb 27, 2004
  2. toton
    Replies:
    23
    Views:
    974
    Chris Uppal
    Nov 2, 2006
  3. Bruno Desthuilliers

    Re: mutate dictionary or list

    Bruno Desthuilliers, Sep 7, 2010, in forum: Python
    Replies:
    0
    Views:
    517
    Bruno Desthuilliers
    Sep 7, 2010
  4. Rick Johnson
    Replies:
    51
    Views:
    655
    Rick Johnson
    Feb 12, 2013
  5. Jean-Michel Pichavant
    Replies:
    0
    Views:
    112
    Jean-Michel Pichavant
    Feb 11, 2013
Loading...

Share This Page