[Tutor] Tuple of Tuples
srilyk at gmail.com
Sat Oct 11 17:12:43 CEST 2008
On Sat, Oct 11, 2008 at 9:53 AM, Wayne Watson
<sierra_mtnview at sbcglobal.net> wrote:
> I would like to create a tuple of tuples. As I read (x,y) pairs I would like
> to "add" them to others. In the example below, I seem to be picking up ()
> each time I augment the tuple t.
>>>> t = ()
>>>> t = t,(0,1)
> ((), (0, 1))
> (((), (0, 1)), (2, 3))
> Why the repeats of ()?
> Maybe I need to use another type? Lists?
Unless you want the tuple to never change, then yes. Tuples are
immutable (you can't change them):
>>> t = (0,2)
>>> t = 4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
What you're doing with this line
>>> t = t,(0,1)
Is the exact same as writing
t = ((), (0,1))
because you're creating a tuple with () and (0,1). Then your next
assignment does something similar:
t = t, (0,5)
is the same as writing
t = ((), (0,1)), (0,5)
A list of tuples would probably be the better way to go.
More information about the Tutor