Unpacking a single tuple _or_ a pair

Peter Bittner bittneph at aston.ac.uk
Mon Mar 13 10:06:45 EST 2000


Hi!

I am trying to unpack single tuples or(!) pairs from a list.
That's my code: (-> it works fine! :)

  for i in range(len(datalist)):
    try:
      value, text = datalist[i]		# separate pair's values
    except:
      text, = datalist[i]	# assume single value on fail
      value = ''
    option = '<option'
    if str(value) != "":
      option = option + ' value="' + str(value) + '"'
    if i == selected_index:
      option = option + ' selected'
    htmlcode = htmlcode + option + '>' + str(text) + '\n'

Question: Is there a 'nicer' way to do this than by tricking with
~~~~~~~~  "try" and "except"?

(--> Please email back to bittneph at aston.ac.uk !!)
Cheers,
Peter

| Peter H. Bittner
| International Student at Aston University
| e-mail: bittneph at aston.ac.uk
| web: http://beam.to/bimbo
+--------------------------



More information about the Python-list mailing list