You can iterate over a list with a list and look for keys in a dictionary, for example
aa = [d[k] for k in f]
Here is an example of his work.
>>> d = {'k1': 1, 'k2': 2, 'k3' :3} >>> f = ['k1', 'k2'] >>> aa = [d[k] for k in f] >>> aa [1, 2]
If you want to restore the dictionary from the result, you can also grab the keys in the list of tuples and convert to dict, for example.
aa = dict ([(k, d[k]) for k in f])
In later versions of Python (in particular, 2.7, 3) there is a function called the concept of dict, which will do it all in one hit. Discussed in more detail here.
source share