Skip to main content
typo; added 5 characters in body
Source Link
Felix Kling
  • 819.8k
  • 181
  • 1.1k
  • 1.2k

You can use enumerate:

for k,i in enumerate(mylist):
    #do something with index k
    #do something with element i

More information about looping techniques.

Edit:
As pointed putout in the comments, using other variable names like

for i, item in enumerate(mylist):

makes it easier to read and understand your code in the long run. Normally you should use i, j, k for numbers and meaningful variable names to describe elements of a list.
IfI.e. if you have e.g. a list of books and iterate over it, then you should name the variable book.

You can use enumerate:

for k,i in enumerate(mylist):
    #do something with index k
    #do something with element i

More information about looping techniques.

Edit:
As pointed put in the comments, using other variable names like

for i, item in enumerate(mylist):

makes it easier to read and understand your code in the long run. Normally you should use i, j, k for numbers and meaningful variable names to describe elements of a list.
If you have e.g. a list of books and iterate over it, then you should name the variable book.

You can use enumerate:

for k,i in enumerate(mylist):
    #do something with index k
    #do something with element i

More information about looping techniques.

Edit:
As pointed out in the comments, using other variable names like

for i, item in enumerate(mylist):

makes it easier to read and understand your code in the long run. Normally you should use i, j, k for numbers and meaningful variable names to describe elements of a list.
I.e. if you have e.g. a list of books and iterate over it, then you should name the variable book.

added 493 characters in body; added 2 characters in body
Source Link
Felix Kling
  • 819.8k
  • 181
  • 1.1k
  • 1.2k

You can use enumerate:

for k,i in enumerate(mylist):
    #do something with index k
    #do something with element i

More information about looping techniques.

Edit:
As pointed put in the comments, using other variable names like

for i, item in enumerate(mylist):

makes it easier to read and understand your code in the long run. Normally you should use i, j, k for numbers and meaningful variable names to describe elements of a list.
If you have e.g. a list of books and iterate over it, then you should name the variable book.

You can use enumerate:

for k,i in enumerate(mylist):
    #do something with index k
    #do something with element i

More information about looping techniques.

You can use enumerate:

for k,i in enumerate(mylist):
    #do something with index k
    #do something with element i

More information about looping techniques.

Edit:
As pointed put in the comments, using other variable names like

for i, item in enumerate(mylist):

makes it easier to read and understand your code in the long run. Normally you should use i, j, k for numbers and meaningful variable names to describe elements of a list.
If you have e.g. a list of books and iterate over it, then you should name the variable book.

Source Link
Felix Kling
  • 819.8k
  • 181
  • 1.1k
  • 1.2k

You can use enumerate:

for k,i in enumerate(mylist):
    #do something with index k
    #do something with element i

More information about looping techniques.