Newbie Q on searching lists

Kenny Tilton ktilton at nyc.rr.com
Sat Nov 23 00:45:55 EST 2002


I see the index method on lists throws an error if the element sought is 
not found. I was hoping it would return None.

The semantics I am coding happen to throw an error if a certain element 
is found in a list, so i want to code:

    if calculators.index[thisCalculator]:
       raise CircularDependency, thisCalculator

Instead I (think) I have to:

    try:
       if calculators.index[thisCalculator]:
          raise CircularDependency, thisCalculator
    except ValueError: pass

Not the end of the world, but more for the sake of getting to know Python:

1. Am I missing something?

2. Can I add a method to List that has the behavior I am after?

3. Should I just start a module of my favorite utilities and create there:

def find( item, list):
   try: yada yada...

4. Other?

-- 

  kenny tilton
  clinisys, inc
  ---------------------------------------------------------------
""Well, I've wrestled with reality for thirty-five years, Doctor,
   and I'm happy to state I finally won out over it.""
                                                   Elwood P. Dowd




More information about the Python-list mailing list