Apr-02-2017, 01:31 PM
Do not use private messages to seek help, use regular posts.
If you need to get indices of all positions of particular term in a list, you canĀ iterate over that list and check its elements and eventually add index to your "list of matching indices". And you can shorten it with a list comprehension:
If you need to get indices of all positions of particular term in a list, you canĀ iterate over that list and check its elements and eventually add index to your "list of matching indices". And you can shorten it with a list comprehension:
Output:>>> a_list = list("sdflksdjfajfd")
>>> a_list
['s', 'd', 'f', 'l', 'k', 's', 'd', 'j', 'f', 'a', 'j', 'f', 'd']
>>> occurences_of_f = [idx for idx, item in enumerate(a_list) if item == "f"]
>>> occurences_of_f
[2, 8, 11]