[Tutor] How many times X is in a list?

Carlos carloslara at web.de
Wed Dec 20 21:59:46 CET 2006


Thanks Kent,

That solves it pretty well.

Carlos



Kent Johnson wrote:
> Yes, count() is looking for exact matches. You can make a new list 
> with just the circulation names and take the length of that; something 
> like this:
> len([name for name in built_Objects if name.startswith('Circulation')])
>
> or perhaps slightly more efficient (Python 2.5):
> sum(1 for name in built_Objects if name.startswith('Circulation'))
>
> Kent


More information about the Tutor mailing list