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

Luke Paireepinart rabidpoobear at gmail.com
Wed Dec 20 19:25:39 CET 2006


> 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'))
>   
I thought sum only worked on lists.
Is that supposed to be a list comprehension inside of sum or am I wrong?
(still using 2.4.3, so I can't check)
Thanks,
-Luke
> Kent
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   



More information about the Tutor mailing list