[Python] I want to use only index when looping a list with a for statement

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

The best way I think


lst = ["A", "B", "C"]
for i, _ in enumerate(lst):
    print(i)

Other ways you don't think it's good

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

[Python] I want to use only index when looping a list with a for statement
I want to use a wildcard that I want to shell with Python remove
I want to use MATLAB feval with python
I want to make a game with Python
I want to use Temporary Directory with Python2
I want to write to a file with Python
I want to work with a robot in python.
[Python] I want to make a nested list a tuple
When you want to plt.save in a for statement
I want to run a quantum computer with Python
[Introduction to Python] How to get the index of data with a for statement
Solution when you want to use cv_bridge with python3 (virtualenv)
[Python] I want to add a static directory with Flask [I want to use something other than static]
[python] A note when trying to use numpy with Cython
[Python] I want to use the -h option with argparse
I want to use a virtual environment with jupyter notebook!
I didn't know how to use the [python] for statement
I want to debug with Python
I know? Data analysis using Python or things you want to use when you want with numpy
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I tried to create a list of prime numbers with python
I want to do a full text search with elasticsearch + python
[Introduction to Python] How to use the in operator in a for statement?
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I want to use jar from python
I want to build a Python environment
I want to analyze logs with Python
I want to play with aws with python
[Short sentence] easygui for those who want to use a simple GUI with Python very easily
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
Gist repository to use when you want to try a little with ansible
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
[Python] I was hooked for an hour trying to use list comprehensions
I want to do a monkey patch only partially safely in Python
I want to create a window in Python
I want to use ceres solver from python
I don't want to use -inf with np.log
#Unresolved I want to compile gobject-introspection with Python3
I want to use ip vrf with SONiC
I want to solve APG4b with Python (Chapter 2)
[Python] How to create a dictionary type list, add / change / delete elements, and extract with a for statement
I want to display only different lines of a text file with diff
I want to create a nice Python development environment for my new Mac
When I tried to create a virtual environment with Python, it didn't work
When I try to use Jupiter notebook on Mac, I can only select python2
A note I was addicted to when running Python with Visual Studio Code
If you want to make a discord bot with python, let's use a framework
A story that I was addicted to when I made SFTP communication with python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to iterate a Python generator many times
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to transition with a button in flask
I want to handle optimization with python and cplex
I want to climb a mountain with reinforcement learning
I tried to draw a route map with Python
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I want to inherit to the back with python dataclass
Things to note when initializing a list in Python