# イテレータのkeyとvalue両方使いたい

超初心者向けです。やりたいことはタイトルの通り。今まではこうしてた↓

```python:python3
>>> dict = {'a': 10, 'b': 20, 'c': 30}
>>> for key in dict.keys():
>>>    print('key: ', key, ' ,value: ', dict[key])
key:  a  ,value:  10
key:  b  ,value:  20
key:  c  ,value:  30
```

for の後ろの変数を二つにして dict.items() を渡せばそれぞれに key, value が入るらしい

```python:python3
>>> dict = {'a': 10, 'b': 20, 'c': 30}
>>> for key, value in dict.items():
>>>    print('key: ', key, ' ,value: ', value)
key:  a  ,value:  10
key:  b  ,value:  20
key:  c  ,value:  30
```

list で似たようなこと（key ではなく index）をしたい場合は enumerate(list) を使用する

```python:python3
>>> l = ['a', 'b', 'c']
>>> for index, value in enumerate(l):
>>>    print('index: ', index, ' ,value: ', value)
index:  0  ,value:  a
index:  1  ,value:  b
index:  2  ,value:  c
```

pandas の Series や DataFrame でも同じようにできますが、index にセットした値は適用されず index=0,1,2,... となります

ちなみに js の Map では同じようなことを分割代入的に書きますよね

---

## Page Navigation

- Canonical URL: https://tips.weseek.co.jp/用語集/Python/イテレータのkeyとvalue両方使いたい
- Permalink: https://tips.weseek.co.jp/5e49380e1ec5e4004811d516
- Parent: [Python](/5e48bfd61ec5e4004811d4f1.md)
- Children: 0 total
- Total descendants: 0
- Siblings: 4 total
  - [テストを書く](/603462f091cd67004906d99f.md)
  - [リスト内包表記](/5e493b681ec5e4004811d51b.md)
  - [拡張IterableUnpacking](/5e492c431ec5e4004811d50a.md)
  - [集合演算](/5e49315f1ec5e4004811d510.md)
- Last updated: 2020-02-16T12:48:25.610Z by deleted_at_1672138409189
- Full page listing (all children regardless of count): https://tips.weseek.co.jp/_api/v3/page-listing/children?id=5e49380e1ec5e4004811d516
