1
2

More than 3 years have passed since last update.

enumerate

Last updated at Posted at 2020-01-05
1
for player in ['勇者', '戦士', '魔法使い']:
    print(player)
1の実行結果
勇者
戦士
魔法使い

これにインデックスも一緒に出力したい場合、

indexも
i = 0
for player in ['勇者', '戦士', '魔法使い']:
    print(i, player)
    i += 1
indexもの実行結果
0 勇者
1 戦士
2 魔法使い

ここでenumerate関数を使えば、
もっと簡単に書ける

enumerate
for i, player in enumerate(['勇者', '戦士', '魔法使い']):
    print(i, player)
enumerateの実行結果
0 勇者
1 戦士
2 魔法使い
1
2
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
1
2