P/S: The approach in best way to extract subset of key-value pairs from python dictionary object is recreating a new subkeys' dictionary. It is slow (I have tried it). The answer of using Subdicview given by shx2 below is great in terms of efficiency.
I have a python dictionary, e.g.,
d={"a1":Obj1, "a2":Obj2,"a3":Obj3,...,"a10":Obj10}
Where Obj1
to Objn
is some objects of self created python class.
The problem is that, in a loop of 100 millions time, I need different subset of keys at each iteration, say I need "a1"
to "a3"
, what I do now is I reconstructed the dictionary
d1={"a1":Obj1, "a2":Obj2,"a3":Obj3}
whenever I want to use it. In the end, I do 100 millions reconstructions of dictionaries.
Is there a more efficient way to handle such case (e.g, muting the keys in d
that I am not interested) without reconstructing the dictionary each time in the loop?