Append tuples to a tuples [duplicate]
Asked Answered
P

6

10

I can do append value to a tuples

>>> x = (1,2,3,4,5)
>>> x += (8,9)
>>> x
(1, 2, 3, 4, 5, 8, 9)

But how can i append a tuples to a tuples

>>> x = ((1,2), (3,4), (5,6))
>>> x
((1, 2), (3, 4), (5, 6))
>>> x += (8,9)
>>> x
((1, 2), (3, 4), (5, 6), 8, 9)
>>> x += ((0,0))
>>> x
((1, 2), (3, 4), (5, 6), 8, 9, 0, 0)

How can i make it

((1, 2), (3, 4), (5, 6), (8, 9), (0, 0))

Patriciapatrician answered 6/12, 2013 at 4:11 Comment(2)
Just note that in this case technically you are not appending - tuples are immutable, you're creating a new tuple there.Bleary
If you need to be able to extend a tuple, are you sure it should be a tuple rather than a list?Plains
S
11
x + ((0,0),)

should give you

((1, 2), (3, 4), (5, 6), (8, 9), (0, 0))

Python has a wonky syntax for one-element tuples: (x,) It obviously can't use just (x), since that's just x in parentheses, thus the weird syntax. Using ((0, 0),), I concatenate your 4-tuple of pairs with a 1-tuple of pairs, rather than a 2-tuple of integers that you have in (0, 0).

Seato answered 6/12, 2013 at 4:12 Comment(0)
I
4

Add an extra parentheses:

>>> x = ((1,2), (3,4), (5,6)) + ((8,9),)
>>> x
((1, 2), (3, 4), (5, 6), (8, 9))

Notice the trailing comma. This will make you add a new-pair tuple to it.

Also, just a note: This is not appending, since tuples are immutable. You're creating a completely whole new tuple.

Hope this helps!

Izawa answered 6/12, 2013 at 4:13 Comment(0)
B
3

[PLEASE SEE NOTE BELOW]

Also this should work:

x += (0,0),

NOTE:

this is unsafe!! Thanks to Amadan and aIKid for the great discussion.

As Amadan pointed out, this specific case will work only because the assignment operator += has lower priority than ,, so by the time the two tuples are joined, (0,0), has already become ((0,0),).

But if you try:

((1, 2), (3, 4)) + (5, 6),

the result will be a messy

(((1, 2), (3, 4), 5, 6),)

because + has higher priority than ,, so the numbers 5 and 6 are joined to the tuple separately! There intermediary stage is then ((1, 2), (3, 4), 5, 6), and finally this tuple with a final , is "corrected" to give (((1, 2), (3, 4), 5, 6),).

Take-home message: using the notation (5, 6), is not safe because it being "corrected" to ((5, 6),) might have lower priority than other operators.

Bleary answered 6/12, 2013 at 4:15 Comment(12)
Python 7.4?! Me wants!! :)Izawa
I'm too new to post a solution without having tried it beforehand, lolBleary
But seriously, you did try it on your system and it didn't work?Bleary
Are you sure? Here's what i get:Izawa
>>> ((1,2)) + (1, 3), Result: ((1, 2, 1, 3),) - Python 2.7.5Izawa
Interesting, looks like this behavior is introduced in Python 2.7.5.Izawa
Oh, weird. x+(0,0), does not evaluate the same as x+=(0,0),.Seato
@Bleary I do know that.Izawa
@Seato Holy cow, this is getting more interesting.Izawa
yup it takes x+(0,0)as first tuple and null as second like (1+2 ,)Omentum
Oh this is weird, I confirm that it doesn't work right at the promptBleary
How about we move to chat? The Python chatroom?Izawa
R
1

in python both ((0,0)) and (0,0) are equal:

>>> x=((0,0))
>>> y=(0,0)
>>> x==y
True

so to get ((0,0)) you need to type ((0,0),)

Roustabout answered 6/12, 2013 at 4:16 Comment(0)
P
1
>>> x = ((1,2), (3,4), (5,6))

>>> x += ((8,9),)

>>> x

((1, 2), (3, 4), (5, 6), (8, 9))

>>> x += ((10,11),(12,13))

>>> x

((1, 2), (3, 4), (5, 6), (8, 9), (10, 11), (12, 13))

To represent a tuple with a single element, you have to use a trailing comma. And unlike list, tuple is immutable

Proton answered 6/12, 2013 at 4:28 Comment(0)
T
0
>>> x = *x, (8, 9); x
((1, 2), (3, 4), (5, 6), (8, 9))

This way you could avoid using the + operator, the += operator, and that weird ((8, 9),) syntax. Using the deconstruct operator * is a bit more elegant and slightly more obvious what you want to have happen: deconstruct previous tuple, appending new element onto new tuple, x.

Traditionalism answered 6/10, 2022 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.