efficient list reduction

Carl Banks imbosol at aerojockey.invalid
Tue May 11 03:14:32 EDT 2004


A B Carter wrote:
> 
> 
> I have two lists. The values of the second list can be viewed as keys
> for the first. I want to create a newlist based on the implied
> mapping. The straight Python code would be:
> 
> newlist=[]
> for key in keys:
>   newlist.append(oldlist[key])
> 
> What's the most efficient way of doing this? The best I could do was
> the following list comprehension:
> 
> [oldlist[key] for key in keys]
> 
> Have I missed something?


newlist = map(oldlist.__getitem__,keys)

Quite a bit faster for me.


-- 
CARL BANKS                      http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work." 
          -- Parody of Mr. T from a Robert Smigel Cartoon



More information about the Python-list mailing list