62

Yes, I understand tuples are immutable but the situation is such that I need to insert an extra value into each tuple. So one of the items is the amount, I need to add a new item next to it in a different currency, like so:

('Product', '500.00', '1200.00')

Possible?

Thanks!

eozzy
  • 66,048
  • 104
  • 272
  • 428
  • 5
    Welcome to functional programming. You're not `updating` a tuple, you're creating a new tuple from an old tuple. Use `collections.namedtuple` and it will be obvious that your're creating a new type of object from an old type of object. – S.Lott Feb 22 '10 at 11:17

8 Answers8

70

You can cast it to a list, insert the item, then cast it back to a tuple.

a = ('Product', '500.00', '1200.00')
a = list(a)
a.insert(3, 'foobar')
a = tuple(a)
print a

>> ('Product', '500.00', '1200.00', 'foobar')
swanson
  • 7,377
  • 4
  • 32
  • 34
62

Since tuples are immutable, this will result in a new tuple. Just place it back where you got the old one.

sometuple + (someitem,)
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
24

You absolutely need to make a new tuple -- then you can rebind the name (or whatever reference[s]) from the old tuple to the new one. The += operator can help (if there was only one reference to the old tuple), e.g.:

thetup += ('1200.00',)

does the appending and rebinding in one fell swoop.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • yes, as i learned the hard way :P += does not attempt to modify the reference in place, it reassigns – Matt Joiner Feb 22 '10 at 07:17
  • @Matt, exactly -- but with a tuple or string, that's all you can do (if you apply it to a list, it _does_ modify it in-place -- the "reassign" part is a negligible overhead), – Alex Martelli Feb 22 '10 at 07:43
  • 1
    Always gets my hackles up. += in my mind should be an in-place modification. If you can't do it, don't appear to be doing it. I'm not the Python spec though, and clearly whatever my opinion, it does what it's defined to do. And it's not an objectively sane complaint anyway - integers are mutable and you can += them easy enough, which again means replacement. Basically intuition-clash between language models. –  Feb 22 '10 at 13:51
  • Oops - integers are *immutable* of course. –  Feb 22 '10 at 14:08
  • @Steve314, if you can do `i+=j` where `i` is immutable by being an `int`, it's clearly irrational to rant against being able to do it when it's immutable by being another type on which `+` is defined. – Alex Martelli Feb 22 '10 at 15:33
  • 1
    In C++ you can + a const int, but not += a const int. I *said* I was being irrational (or rather "not objectively sane"), and I also said "intuition-clash between language models". And no-ones ranting - I was simply commenting. Though I may well have been ranting in the past when we had a related discussion on comp.lang.python. –  Feb 23 '10 at 12:13
20
def tuple_insert(tup,pos,ele):
    tup = tup[:pos]+(ele,)+tup[pos:]
    return tup

tuple_insert(tup,pos,9999)

tup: tuple
pos: Position to insert
ele: Element to insert

Vidya Sagar
  • 1,496
  • 14
  • 23
12

For the case where you are not adding to the end of the tuple

>>> a=(1,2,3,5,6)
>>> a=a[:3]+(4,)+a[3:]
>>> a
(1, 2, 3, 4, 5, 6)
>>> 
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
7

You can code simply like this as well:

T += (new_element,)
Vivek
  • 192
  • 1
  • 3
  • 7
7
t = (1,2,3,4,5)

t= t + (6,7)

output :

(1,2,3,4,5,6,7)
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
hardik patel
  • 134
  • 1
  • 9
4

one way is to convert it to list

>>> b=list(mytuple)
>>> b.append("something")
>>> a=tuple(b)
ghostdog74
  • 327,991
  • 56
  • 259
  • 343