How to extend a tuple of tuples?
Rustom Mody
rustompmody at gmail.com
Fri Sep 9 03:07:38 EDT 2016
On Friday, September 9, 2016 at 12:18:24 PM UTC+5:30, Frank Millman wrote:
> Hi all
>
> This should be easy, but I cannot figure it out.
>
> Assume you have a tuple of tuples -
>
> a = ((1, 2), (3, 4))
>
> You want to add a new tuple to it, so that it becomes -
>
> ((1, 2), (3, 4), (5, 6))
Your example does not add inside the inner tuples
So I am simplifying the question to
> Assume you have a tuple of tuples -
>
> a = (1 2 3)
>
> You want to add a new element to it, so that it becomes -
>
> (1 2 3 4)
>>> t = (1,2,3)
>>> new = t + (4,)
>>> new
(1, 2, 3, 4)
>>>
Slightly harder if the new addition were inbetween
>>> t = (1,2,3)
>>> t[:1]
(1,)
>>> t[1:]
(2, 3)
>>> t[:1] + (42,) + t[1:]
(1, 42, 2, 3)
More information about the Python-list
mailing list