On Jun 17, 11:19 pm, ssc <steven.samuel.c...@gmail.com> wrote: > Hello, > > I am trying to generate this list of tuples: > [(0, ''), (1, 'Dr'), (2, 'Miss'), (3, 'Mr'), (4, 'Mrs'), (5, 'Ms')] > > My code works fine in the Python shell: > > >>> titles = ['Dr', 'Miss', 'Mr', 'Mrs', 'Ms',] > >>> title_choices = [(0, '')] + list((titles.index(t)+1, t) for t in titles) > >>> title_choices > > [(0, ''), (1, 'Dr'), (2, 'Miss'), (3, 'Mr'), (4, 'Mrs'), (5, 'Ms')] > > The same code run in a script fails with > NameError: global name 'titles' is not defined > > Does anybody know why ? How can I fix the error ? > > Thank you very much :-) > > Steve
Why are you doing this? I'm assuming a code to title look up is required (don't forget military, royal and honorable titles etc... :) ) Why not just: >>> titles = ['', 'Dr', 'Miss', 'Mr', 'Mrs', 'Ms',] >>> lookup = dict(enumerate(titles)) >>> print lookup {0: '', 1: 'Dr', 2: 'Miss', 3: 'Mr', 4: 'Mrs', 5: 'Ms'} or if you don't want a dict, then just: >>> print list(enumerate(titles)) [(0, ''), (1, 'Dr'), (2, 'Miss'), (3, 'Mr'), (4, 'Mrs'), (5, 'Ms')] -- http://mail.python.org/mailman/listinfo/python-list