Python - How to return the indice value of a list? -
currently have
element in initialstring: if element == word: print(element.index(initialstring))
where initialstring
list , word
called variable.
however, returning typeerror
telling me cannot convert 'list' object str implicity
.
you'd use initialstring.index(element)
(so reversed) instead; list can tell index has element
stored in; element
has no knowledge of list.
you should use enumerate()
here however, add indices loop:
i, element in enumerate(initialstring): if element == word: print(i)
if wanted know index of word
, however, simpler still use:
print(initialstring.index(word))
Comments
Post a Comment