Given a list [\"foo\", \"bar\", \"baz\"]
and an item in the list \"bar\"
, how do I get its index (1
) in Python?
For those coming from another language like me, maybe with a simple loop it's easier to understand and use it:
mylist = ["foo", "bar", "baz", "bar"]
newlist = enumerate(mylist)
for index, item in newlist:
if item == "bar":
print(index, item)
I am thankful for So what exactly does enumerate do?. That helped me to understand.