Newbie Q on searching lists
Sean 'Shaleh' Perry
shalehperry at attbi.com
Sat Nov 23 02:32:10 EST 2002
On Friday 22 November 2002 21:45, Kenny Tilton wrote:
> 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
>
> 1. Am I missing something?
>
if calculators.count(thisCalculator): # returns zero if not found
raise CircularDependency, thisCalculator
print count.__doc__
count(value) -> integer -- return number of occurrences of value
> 2. Can I add a method to List that has the behavior I am after?
>
in the new python 2.2 code base you can derive from list and add a new method
> 3. Should I just start a module of my favorite utilities and create there:
>
> def find( item, list):
> try: yada yada...
>
not always a bad option either
More information about the Python-list
mailing list