When looping a list with a for statement in Python, I think that it is generally when you want to use each element of the list. However, there are times when you want to use only the ** index ** of the list. No, not so much in general, but at least I do. I will leave the method that I think is the best below.
The best way I think
lst = ["A", "B", "C"]
for i, _ in enumerate(lst):
print(i)
for index, item in enumerate(lst)
Bot 1
lst = ["A", "B", "C"]
for i, item in enumerate(lst):
print(i)
The purpose can be achieved with this example as well, but I have used extra items that are not used in the loop. It's a little burden. Also, a note saying ʻUnused variabe'item'` is displayed (when using VS Code). It's annoying. It's a bot.
for index in range(len(lst))
Bot 2
lst = ["A", "B", "C"]
for i in range(len(lst)):
print(i)
This is fine, but it doesn't actually loop lst. I'm kind of lonely, so I'm sorry.
Recommended Posts