Search index list in a loop

I have a simple question. If I have a for loop in python as follows:

for name in nameList:

How to find out what an index is for an element name? I know I can do something like:

i = 0
for name in nameList:
    i= i + 1
    if name == "something":
        nameList[i] = "something else"

I just feel that there should be a more readable way to do this ...

+3
source share
1 answer

Use the built-in function enumerate.

for index, name in enumerate(nameList):
    ...
+10
source

Source: https://habr.com/ru/post/1761149/


All Articles