辞書のサブセットを取得する
d = {"a": 1, "b": 2, "c": 3} print {k: d[k] for k in ('a', 'b', 'd') if k in d} # {'a': 1, 'b': 2} print d # {'a': 1, 'c': 3, 'b': 2}
参考 : best way to extract subset of key-value pairs from python dictionary object - Stack Overflow
d = {"a": 1, "b": 2, "c": 3} print {k: d[k] for k in ('a', 'b', 'd') if k in d} # {'a': 1, 'b': 2} print d # {'a': 1, 'c': 3, 'b': 2}
参考 : best way to extract subset of key-value pairs from python dictionary object - Stack Overflow