Newbie Question: Giving names to Elements of List/Tuple/Dict

Alfredo P. Ricafort alpot at mylinuxsite.com
Fri Nov 29 06:56:44 EST 2002


Hi,

I'm quite new to Python.  So far I find it to be easy to learn and
simple to program.  However, one thing that I missed, especially someone
coming from C language, is 'struct'. It seems that when you define a
data structure in the form of a List, Tuple, or Dict., there is no way
to give names to each element. 

For example:

In C:
   struct Customer {
          char * Name;
          char * Address;
          char * TelNo;
   } Customer[];
  
   printf("Customer Name is %s\n",Customer[i].Name); 
   

In Python:
   Customer=[ [Name,Addres,TelNo], [Name,Address,TelNo],.....]
 
   print "Customer Name is %" Customer[i][0]

Now my problem is that when the structure of the record change, your
index has to change also. So in the example above, when the 'Name'
element is moved(say as the 3rd element - Customer[i][2]), then you have
to look all over your code and change accordingly. 

My question now is, how can I assign names to the elements? Or is there
a better way of doing this?

Thanks.

AL




More information about the Python-list mailing list