efficient list reduction

A B Carter gnosticray at aol.com
Tue May 11 03:01:20 EDT 2004


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?

Regards, A B Carter



More information about the Python-list mailing list