[Tutor] counting a list of elements
James Reynolds
eire1130 at gmail.com
Fri Apr 1 20:10:52 CEST 2011
The nice thing about Python is you don't have to build things from scratch,
and I imagine most people would discourage on account of it being a waste of
time, other then education value.
But the "best practice" in my humble opinion would be to use the built in
len function. If what you are after is to see how often 1e3 comes up in your
list, then you would use list.count('1e3').
In general, its best to use the python built ins.
On Fri, Apr 1, 2011 at 1:16 PM, Karim <karim.liateni at free.fr> wrote:
>
>
> Hello,
>
> I would like to get advice on the best practice to count elements in a list
> (built from scractch).
> The number of elements is in the range 1e3 and 1e6.
>
> 1) I could create a generator and set a counter (i +=1) in the loop.
>
> 2) or simply len(mylist).
>
> I don't need the content of the list, indeed, in term of memory I don't
> want to wast it. But I suppose len() is optimized too (C impementation).
>
> If you have some thought to share don't hesitate.
>
> Karim
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110401/cac3276a/attachment.html>
More information about the Tutor
mailing list