Accessing items in nested tuples

Rhodri James rhodri at wildebst.demon.co.uk
Wed Apr 22 18:11:24 EDT 2009


On Tue, 21 Apr 2009 21:45:21 +0100, alex <alecla at bluewin.ch> wrote:

> Tim and Mensanator
> Thank you very much, this will get me further.
> I can not recall having seen this in my books...
> I am still getting my grips with Python and OOP.

Think of it this way, Alex:  data[0] gave you back the tuple
('aa', ('bb', 'cc', 'dd')).  You can treat that tuple in exactly
the same way as any other tuple, it just happens to have the
"name" data[0].  If you want to get its first element, you
stick [0] after the name, just like indexing any other tuple,
making it data[0][0].

This generalises.  You can use whatever object data[0] gives
you exactly like any other use of that type of object.  If it
gives you a dictionary, you can write:

   data[0]['spam'] = "moist and delicious"

and so on.

-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list