1

This is my dataset:

dataset = [['A','B','C], ['D','E','F']]

I want the INDEX in this for loop.

for x in dataset:
  print ?????

I also tried this and many other things:

print(x.index())
prit(index(x))
etc..

All return errors.

2 Answers 2

3

Use enumerate as follows :

for ind, x in enumerate(dataset) :
  print(ind)
Sign up to request clarification or add additional context in comments.

4 Comments

Ok thanks a lot. Is it possible also to access the value of the item?
LOL, I get it !
If this answer fits you, I'd appreciate you validate it please ;)
Sure, I was expecting - points like hell, because stack overflow hates begginers so I'll accept it for sure!
0

To make the answer more understandable because i'm not a fan of 'use this function it just works' (it's efficient but usually lacks teaching) the enumerate funtions works just like:

#values = your list
index = 0
for value in values:
    print(index, value)
    index += 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.