Dictionary vs if-elif?

William Dandreta wjdandreta at worldnet.att.net
Tue Jun 20 18:36:36 EDT 2000


Here is a code snippet
-------------------------------------------------------------------------
bogusCodes = { '1.':'', '31':'', '90':'', 'AZ':'', 'BH':'', 'C1':'',
               'CB':'', 'FR':'', 'HD':'', 'JF':'', 'KL':'', 'KU':'',
               'LU':'', 'ON':'', 'PF':'', 'Q3':'', 'UP':'', 'W4':'',
               'WO':''}

item  = data.readline()
while item:
  if (item[1:2] in digits) and (item[0:1] == 'D'):
    goodVend.write(item)
  elif vendors.has_key(item[0:2]):
    goodVend.write(item)
  elif bogusCodes.has_key(item[0:2]):
    pass
  else:
    missingVend.write('Vendor code %s is missing from file VendCode.txt\n' %
item[0:2])
  item  = data.readline()
---------------------------------------------------------------
originally I had it written like so:

if ...:
  pass
elif...:
  pass
etc.
-------------------------
another possibility is and if statement with a very complex condition:
if a or b or c ...:
  pass
else :
etc
---------------------
Is there a better way to do it?
Which of these three would be the preferred method?

Bill





More information about the Python-list mailing list