Beginners Question - use of variables

Magnus Lyckå magnus at thinkware.se
Tue Sep 17 14:56:38 EDT 2002


> 
> my_list = [
>            ['sean', 'perry', 25, '20020906'],
>            ['Martin', 'Klaffenboeck', 20, '20020826'],
>           ]
> 
> for entry in my_list:
>     print entry[0], entry[2] # prints first string and the int

A more Pythonish solution is to use a list of tuples,
rather than a list of lists. Tuples are a better choice
for data structures that look like records or structs.
A tuple is not mutable, and it is more lightweight. I.e:

my_list = [
            ('sean', 'perry', 25, '20020906'),
            ('Martin', 'Klaffenboeck', 20, '20020826'),
           ]

for entry in my_list:
     print entry[0], entry[2] # prints first string and the int

The most memory compact storage type (in case you have very big lists)
is probably achieved by using the struct module. See the library
reference, section 4.3.




More information about the Python-list mailing list