You can catch a ValueError exception, or you can do:
i = intList.index(13) if 13 in intList else -1
(Python 2.5+)
BTW. if you are going to do a large batch of such operations, you might consider creating an inverse dictionary value -> index.
intList = [13,1,2,3,13,5,13] indexDict = defaultdict(list) for value, index in zip(intList, range(len(intList))): indexDict[value].append(index) indexDict[13] [0, 4, 6]
vartec
source share