Search code examples
pythonfor-looppep8

Python for-loop without index and item


Is it possible in python to have a for-loop without index and item? I have something like the following:

list_1 = []
for i in range(5):
    list_1.append(3)

The code above works fine, but is not nice according to the pep8 coding guidelines. It says: "Unused variable 'i'".

Is there a way to make a for-loop (no while-loop) without having neither the index nor the item? Or should I ignore the coding guidelines?


Solution

  • You can replace i with _ to make it an 'invisible' variable.

    See related: What is the purpose of the single underscore "_" variable in Python?.