LoginSignup
37
29

More than 5 years have passed since last update.

Python: キーと値のリストから辞書を作成する

Posted at
keys = ['a', 'b', 'c']
values = [10, 20, 30]

dic = dict(zip(keys, values)) # => {'a': 10, 'b': 20, 'c': 30}

dict(iter) (iter は iterable オブジェクト) は,iter の各要素について iter[i][0] をキー,iter[i][1] を値とした辞書を返す (以下のコードと等価).len(iter[i]) != 2 なら ValueError になる.

dic = {}
for k, v in iter:
    dic[k] = v
37
29
0

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
37
29