🖥

#python で配列を持ったディクショナリのキーがある場合は for ループを回して、逆にない場合は何もしない例

に公開
2023/08/29

get の第二引数に初期値の配列を渡しておく

( some_dict.get('some_key', []) )

people = {'family': ['alice','bob','carol']}

for person in people.get('family', []):
  print(person)

# alice
# bob
# carol

for person in people.get('workers', []):
  print(person)

# (nothing)

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/1925

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-05-19

Discussion