0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python | enumerate関数

Posted at

はじめに

  • enumerateとはなにか
  • 従来の書き方
  • enumerate関数を使った記法

enumerateとはなにか

enumerateとは、'列挙する'という意味です。

forループなどでインデックス(数字)を打つときなどに用いることがあります。

従来の書き方

i = 0

for yaki in ['takoyakki', 'okonomiyaki', 'monjayaki']:
    print(i, yaki)
    i += 1
    
# conclusion

'''
0 takoyakki
1 okonomiyaki
2 monjayaki
'''

iに0を代入して、forで回すたびにインデックス番号を打つという方法がありますが、これらはenumerateを用いることで、簡単にかけます。

enumerate関数を使った記法

for i, yaki in enumerate(['takoyakki', 'okonomiyaki', 'monjayaki']):
    print(i, yaki)

# conclusion

'''
0 takoyakki
1 okonomiyaki
2 monjayaki
'''

このように、enumerateを使うことで、簡単にインデックス番号を付けることができます。

0
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?