Python: how to slice a dictionary based on the values of its keys?
问题 Say I have a dictionary built like this: d={0:1, 1:2, 2:3, 10:4, 11:5, 12:6, 100:7, 101:8, 102:9, 200:10, 201:11, 202:12} and I want to create a subdictionary d1 by slicing d in such a way that d1 contains the following keys: 0, 1, 2, 100, 101, 102 . The final output should be: d1={0:1, 1:2, 2:3, 100:7, 101:8, 102:9} Is there an efficient Pythonic way of doing this, given that my real dictionary contains over 2,000,000 items? I think this question applies to all cases where keys are integers,