storing variable names in a list before they are used?

MonkeeSage MonkeeSage at gmail.com
Fri Sep 29 16:43:14 EDT 2006


John Salerno wrote:
> If I want to have a list like this:
>
> [(first_name, 'First Name:'), (last_name, 'Last Name:').....]

Do you need the data to be ordered? If not, just use a dictionary:

d = {'First Name:': '', 'Last Name:': ''}
d['First Name:'] = 'Bob'
d['Last Name:']  = 'Smith'
print "Hi, I'm %s %s." % (d['First Name:'], d['Last Name:'])

If so, check out the ordered dictionary module [1]:

from odict import OrderedDict as odict
od = odict([('First Name:', ''), ('Last Name:', '')])
od['First Name:'] = 'James'
od['Last Name:']  = 'Bond'
for k,v in od.items():
  print "%s => %s" % (k,v)

[1] http://www.voidspace.org.uk/python/odict.html

Regards,
Jordan




More information about the Python-list mailing list