[Tutor] Faster procedure to filter two lists . Please help

Tim Peters tim.peters at gmail.com
Sun Jan 16 00:59:33 CET 2005


[Gonçalo Rodrigues]
> It this correct? Python lists are not linked-lists (as in Scheme, for
> example). They are more like arrays (or vectors in C++/Java) with a
> little more sofistication built into them to allow, for example, to
> amortize over time a sequence of append operations. But in a nutshell,
> len is actually a field in the underlying C object so len() is a
> constant (O(1)) and as-fast-as-it-can-be operation.
>
> Someone correct me, please, If I'm wrong.

That's all correct.  It remains more idiomatic to do

    for thing in a_list:
        ...

than

    for i in range(len(a_list)):
        thing = a_list[i]
        ...

and the former is in fact a bit faster (or possibly a lot, if a_list
is very large, because range(n) actually constructs a list containing
n integers), but (ignoring the range() complication) there's no
difference in O() behavior between the two.


More information about the Tutor mailing list