adam tsang adam tsang - 1 year ago 90
Python Question

Python list finding with index

I was wondering how would you find the index of a element in a list if you only had part of it. For example

list = ["this", "is", "an", "example", "for", "python"]

how would you find it if you only had
and needed the index for python???

Answer Source

Straightforward for loop:

def find_starts(val, my_list):
    for i, v in my_list:
       if v.startswith(val):
           return i

(If there is no match then this returns None.) This could be made neater with a list comprehension: see this related question for more details.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download