-5

i've done a bit of code that basically finds the index of words in a sentence but i don't really understand what i've done and the what is the function of the enumerate ?

    sentence = ['The', 'cat','sat', 'on', 'the', 'mat']
    for index, word in enumerate(sentence):
        print (index, word)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

1

the documentation will tell you more then you need to know:

Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration. The __next__() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable.

Equivalent to:

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59