Issue
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))
Solution
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)
.
Answered By - Amadan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.