Reputation: 133
Hi assuming I have the following list:
words = ['maritus', 'et', 'quolibet', 'is', 'habitancium', 'dico', 'locum~locus', 'domus', 'totus', 'tempus', 'vitis', 'is', 'de', 'quolibet', 'ipse']
and the following word: locus
and I want to get the index of where it is found in words ie: 6 the following:
ind = words.index('locus')
will only work for whole words
Upvotes: 1
Views: 1485
Reputation: 20434
Use next()
on a generator that yields each word in the words
list that locus
is in
. However, since you want the index, you should enumerate
the words
list and only yield the index, i
.
next(i for i, w in enumerate(words) if 'locus' in w)
#6
To get the indexes of all the occurrences, we can use a list-comprehension
:
[i for i, w in enumerate(words) if 'locus' in w]
Upvotes: 3