0
0

More than 1 year has passed since last update.

【Python】辞書から部分辞書の作製

Last updated at Posted at 2022-07-09

やりたいこと

ある辞書から、キー指定で一部の要素を抽出した辞書を作成したい。

具体的にはdic_motoからlist_keyを使ってdic_kekkaを作成する。

dic_moto = {'A':1, 'B':2, 'C':3}
list_key = ['A', 'B']
dic_kekka =  {'A':1, 'B':2}

方法

dic_moto  = {'A':1, 'B':2, 'C':3}
list_key  = ['A', 'B']
dic_kekka = {key:value for key, value in dic.items() if key in list_key}

おわりに

ググっても見つからなかったので、メモ代わりに。

辞書内包表記ができることを初めて知った。

他に良い方法があれば是非教えてください。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0