A summary of answers questions

Tim Peters tim_one at email.msn.com
Wed May 26 23:08:45 EDT 1999


> ...
> Some other options:
>
>    new_list = map(lambda x: list[x], indexes)
>
> Now this I like! clean, simple, and, I imagine, fast.

Likely the slowest method of all -- incurs the overhead of a Python-level
function call for every list element.

> Here's the "obscure one-liner":
>
>    import operator
>    list = [1,2,3,4,5,6,7,8,9,10,11]
>    indices = [3,5,9]
>    new_list = map (operator.getitem, len (indices) * [list], indices)
>
> This certainly works, but it builds a list that is
> len(indices)*len(list) long, which could be pretty darn big, so it
> doesn't seem like a very efficient way to go.

Much faster than the lambda -- things aren't always as they appear <wink>.
The subexpression constructs one list of length indices, each element of
which is (under the covers) a pointer to (the same) list.

This calls operator.getitem for each element in the list too, but
operator.getitem is a C function so doesn't incur the expense of calling a
Python function.  That's what makes this so much faster than the lambda
version.

if-guido-had-spelled-lambda-"adbmal"-people-would-expect-it-to-
    be-slow-ly y'rs  - tim






More information about the Python-list mailing list