LoginSignup
0
1

More than 3 years have passed since last update.

Pythonのforループでインデックス番号を取得する

Posted at

enumerate関数はカウントと値をタプルで返却する。
https://docs.python.org/ja/3/library/functions.html#enumerate

sample.py
seasons = ['Spring', 'Summer', 'Fall', 'Winter']
print(list(enumerate(seasons)))
>> [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]

enumerate関数を利用してforループでインデックス番号を取得できる。

sample.py
for i, v in enumerate(seasons):
    print(i, v)
>> 0 Spring
>> 1 Summer
>> 2 Fall
>> 3 Winter
0
1
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
0
1